我正在考虑在db中设计一个存储序列化对象的字段。当我在实体中调用该属性时,返回明显的String属性。我正在寻找一种动态附加属性的方法,并将反序列化的对象分配给Class实例。任何人都可以建议最好的方式吗?
UserId
......... .......用户注释(nvarchar)
[Serializable]
[XmlRoot("Notes")]
public class GenericNotes {
public DateTime Date {
get;
set;
}
public String CommentBy {
get;
set;
}
public string Type {
get;
set;
}
public string Comment {
get;
set;
}
}
public class Users {
public UserId int {
get;
set;
}
public string UserNotes {
get;
set;
}
// I dont have the following definition in the class because its coming from entity framework.
//But I want the following property attached to the class on runtime.
//I will take care of of deserializing using extension methods or some sort methods.
public string List < GenericNotes > NotesCollection {
get;
set;
}
}
答案 0 :(得分:0)
您可以使用扩展方法来代替属性
public static class UserExtension
{
public static List<GenericNotes> GetNotes(this Users users)
{
//return your deserialized GenericNotes from string
}
}
然后你可以在任何地方使用它
List<GenericNotes> notes = users.GetNotes();