开发者

How do I get property value from property name in Linq to Sql?

I have generic repository. There is a method Save() to persist entity in db. I want to check primary key through Table.GetPropertyValue("ID", entity) - how i should implement it?

private Table<T> Table
{
    get { return db.GetTable<T>(); }
}

public void Save(T entity)
{
    if (Table.GetProper开发者_运维技巧tyValue("ID", entity) == 0)
    {
         Add(entity);
    }
    else if (Table.GetOriginalEntityState(entity) == null)
    {
         Table.Attach(entity);
         Table.Context.Refresh(RefreshMode.KeepCurrentValues, entity);
    }
    db.SubmitChanges();
}


You can use dynamic in C# 4:

if (((dynamic)entity).ID == 0)
    ....


you could use reflection, but for this use case i'd recommend creating an interface:

public interface IHasId
{
    public int ID{get;}
}

make all the relevant tables implement this interface (in the partial class definition), then you can try and cast entity into an IHasId


Say I have a class B defined as follows:

public class B
{
    public int Id { get; set; }
}

Then you could get the value of the id like this:

var b = new B();
b.Id = 60;
int id = GetId(b);

with the GetId method defined as follows:

    public static int GetId(object o)
    {
        var idProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == "Id");

        if (idProperty == null)
            throw new ArgumentException("object does not have an Id property", "o");

        if (idProperty.PropertyType.FullName != typeof(Int32).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type Int32", "o");

        return (int)idProperty.GetValue(o, new object[] { });
    }

A more generic solution would be to make a method like this:

    public static T GetProperty<T>(object o, string propertyName)
    {
        var theProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == propertyName);

        if (theProperty == null)
            throw new ArgumentException("object does not have an " +  propertyName + " property", "o");

        if (theProperty.PropertyType.FullName != typeof(T).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type " + typeof(T).FullName, "o");

        return (T)theProperty.GetValue(o, new object[] { });
    }

which would be called like so:

var b = new B();
b.Id = 60;
int id = GetProperty<int>(b, "Id");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜