好了,所以......
的主要問題是,我有這個類:處理object
鍵時
public class UniqueEntity {
[key]
public object Key { get; set; }
}
但EntityFramework
弄亂表的創建。我的想法是,關鍵可以是任何類型,由子類指定。
我開始使用泛型:
public class UniqueEntity<T> where T : IComparable, IComparable<T>
{
[Key]
public T Key { get; set; }
}
這在一開始效果不錯,但導致了嚴重的設計問題,後來創建我的通用存儲庫(庫的定義是一樣IRepository<T,E>
,當IRepository<E>
就足夠了的時候。
所以,EntityFramework
使用與Key
屬性註釋,以獲得實體的主要類型物業的PropertyInfo
值。如果只有我可以將該類型更改爲用戶希望它(在運行時),並避免使用泛型,它會很棒。
事實證明,你不能刪除屬性,或帶有自定義屬性更改其類型buuuuuut ..實際上,你可以創建一個新的虛擬財產,並從原來的Key
屬性中刪除屬性!
爲此,我們需要一個CustomReflectionContext
(System.Reflection。上下文):
public class Modifier : CustomReflectionContext
{
private string propName;
private object propValue;
public Modifier(string propName, object propValue)
{
this.propName = propName;
this.propValue = propValue;
}
protected override IEnumerable<PropertyInfo> AddProperties(Type type)
{
Type newType = MapType(propValue.GetType().GetTypeInfo());
return CreateProperty(newType, propName, o => propValue, (o, v) => propValue = v,
new Attribute[] { new KeyAttribute() }, new Attribute[] { }, new Attribute[] { });
}
protected override IEnumerable<object> GetCustomAttributes(MemberInfo member, IEnumerable<object> declaredAttributes)
{
return new Attribute[] { };
}
}
有了這個小類的幫助下,我們能夠創建自己的自定義類型給予EntityFramework
,並使用具有現在[Key]
標註一個新的屬性:
var context = new Modifier("NewKey", 0);
var uniqueEntityType = context.MapType(typeof(UniqueEntity).GetTypeInfo());
var keyType = uniqueEntityType.GetProperty("Key").PropertyType;
var keyAttrs = uniqueEntityType.GetProperty("Key").GetCustomAttributes();
var newKeyType = uniqueEntityType.GetProperty("NewKey").PropertyType;
var newKeyAttrs = uniqueEntityType.GetProperty("NewKey").GetCustomAttributes();
Console.WriteLine("Key Property. Type: {0}, Attribute: {1}", keyType, keyAttrs.FirstOrDefault());
Console.WriteLine("NewKey Property. Type: {0}, Attribute: {1}", newKeyType, newKeyAttrs.FirstOrDefault());
Console.ReadLine();

備案。我想這樣做,因爲'Key'將被標註一個屬性。外部框架將識別該屬性並從'PropertyType'中獲取它的類型。如果返回'System.Int32'而不是'System.Object',將會很棒。 – 2014-11-24 05:00:08
有趣的問題。我不相信你可以在運行時做到這一點。但作爲一個對象,你可以在其中存儲任何東西,然後做運行時投射。 – 2014-11-24 05:04:43
不可能是你想要的,但是你可以繼承這個類並創建新的屬性來轉換該屬性,而你只使用新的屬性。 – 2014-11-24 05:05:30