我想使用注释来映射带有sql列的类属性,然后我编写了这段代码,但是我不知道这是否正确,我想添加一个字典项目,使用注释中使用的名称作为键,但是我不知道以下代码是否是执行此操作的最快方法,例如,有没有一种方法可以在不指定字符串的情况下将该属性放入该属性中? (this.GetType()。GetProperty(“ Id”)
public class Key : Attribute {
public string Key { get; set; }
public Key(string key) {
Key = key;
}
}
-
public class Example {
public IDictionary<string,object> Attributes = new Dictionary<string,object>();
[Key("id")]
public Guid Id {
get { return (Guid) Attributes[((KeyName)this.GetType().GetProperty("Id").getcustomAttributes(typeof(Guid),true).First()).Key]; }
set { Attributes.Add(((KeyName)this.GetType().GetProperty("Id").getcustomAttributes(typeof(Guid),true).First()).Key,value); }
}
}