Click here to Skip to main content
15,877,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to add a properties in model(class) via dynamically. How to affect orignal class file. The newly added properties need to present in the class file after complie. i.e. we need to rewrite the class file for add remove properties.


EX:

File Name => stud.cs

Orignal
C#
class stud
{
 public string Id {get;set;}
 public string Name {get;set;}
}


I need to add via code,
another property like Sch_Id.

Thanks.
Posted

You cannot. The only way to do so is to emit the whole assembly with your types using System.Reflection.Emit, but this feature is quite difficult to use; and it is designed for different purposes.

I can tell you the right approach, but not sure if you can use it.

Instead of using "real" properties, you should create a universal data model which models objects having any thinkable properties. Thiins is called metadata. You can model the concept of meta-type, instances of this type will describe which properties are there. The other type should model the instances of this meta-type. Object of this type (call it meta-instance) will have the meta-properties described in meta-type, but carry the individual value of them.

In simple case, let's describe the situation where you want to have some variable number of properties, but each property is just of the string type. This can be represented by the list of names, each name representing meta-property name. So, the meta-type will have just the list of names. The type representing instances of this meta-type, will have the reference to meta-type and appropriate number of properties. Those properties could be implemented as the System.Collections.Generic.Dictionary<string, string>, the dictionary string representing the property name, and the value — the respective value of the property. (This is how Javascript properties and arrays work; and this is how Javascript works as the "loose-type" language, but I am explaining the "strict-typing" meta-modeling.) As your meta-instance should implement strict meta-typing model (remember, you keep reference to meta-type), the dictionary size matches the list size (property list) of the meta-type. The keys in the dictionary are redundant, they are needed for fast O(1) search of the property. However, you can implement it is some other way, say, based on the indices of corresponding property.

It needs some work and thinking, but it can give you the system with number and names of such meta-properties modifiable during runtime, because you can always create a different instance of meta-type during runtime.

—SA
 
Share this answer
 
Comments
johannesnestler 25-Feb-14 9:52am    
Very good advice, I once walked down (abused) the "Emit"-path for a similar scenario (metadata approach was difficult because visual designer was involved), in the end it worked, but as you said it was a pain. - Big mistake not to ask you first ;-)
Sergey Alexandrovich Kryukov 25-Feb-14 10:19am    
Next time, I will gladly help if I can. :-)
This is one of the reasons why visual designed should not be over-used. There are many cases where writing code is much better. If you think about it: creation of code using the designer means manual annoying work, no reuse at all...
—SA
Sergey Alexandrovich Kryukov 26-Feb-14 0:23am    
Who are you talking to? If this is me, none of my 3 names matches "Alex"...
—SA
Sergey Alexandrovich Kryukov 26-Feb-14 0:27am    
First of all, there is no such concept as "overwrite the class". What would it be? When you create a derived class, you can add members, but this is not what you were asking about.
"XML to Class File" does not mean anything certain, you would need to specify what do you mean. What is "class file"? There is not such special concept. Files are pretty much irrelevant to C# code...
—SA
Well you can't do that.
If you might have noticed, Class is a template, that allow us to instantiate objects from it.

It doesn't allow us to persist something. It is just a Signature or as I said, Template.
With the help of this template you can create instances of it, call methods of it, and/or may use static variables/functions of it. But still you can't hold data on it.
Well yeah, you can certainly store data somewhere i.e Text Files, Databases etc using Class.

So the solution towards your problem is, create a class, which can have certain properties that you need i.e Id, Name etc.
If you're not using any data storage, then you can store these data on List<t></t>, or Arrays etc., and use it.

Like,
C#
List<Student> studs = new List<Students>();
studs.Add(new Student {Id = 1, Name = "AAA"});
studs.Add(new Student {Id = 2, Name = "BBB"});
studs.Add(new Student {Id = 3, Name = "CCC"});

and if you want to get the data of Student, which has Id=1 then,
C#
Student s = studs.Find(s => s.Id == 1);
Console.Write(s.Name);

Rather I'd suggest you to go for Databases. :)

-KR
 
Share this answer
 
v2
This is not possible yet but if you want to add some property that can initialize at run time you can do like

C#
class stud
{
 public string Id {get;set;}
 public string Name {get;set;}

 public string _newVar;

 public string newVar
 {
   get { return _newVar; }
 }
 
 public void SetValueFornewVar(string val)
 {
   _newVar = val;
 }
}

//Then you can initialize the newVar anywhere like
stud studObj = new stud();
stud.SetValueFornewVar("Hello World");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900