简体   繁体   中英

Dynamic property to a C# object

I am thinking designing a field in db that stores the Serialized Object. When I call that property in entity, that returns String property which is obvious. I am looking for a way to attach a property dynamically and assign the deserialized object to the Class instance. Can any one suggest the best possible way?

DB Structure

Users Table

UserId
.....
.....
.....
UserNotes (nvarchar)

Class Structure

  [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;
     }
 }

Instead of property you can have extension method to do this

public static class UserExtension
{
    public static List<GenericNotes> GetNotes(this Users users)
    {
        //return your deserialized GenericNotes from string 
    }
}

Then you can use this anywhere like

 List<GenericNotes> notes = users.GetNotes();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM