如何以动态方式向模型添加属性 [英] How to add properties to model dynmaically

查看:113
本文介绍了如何以动态方式向模型添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要动态地在模型(类)中添加属性。如何影响orignal类文件。新添加的属性需要在complie之后出现在类文件中。即我们需要重写类文件以添加删除属性。





EX:



文件名=> stud.cs



Orignal

  class  stud 
{
public string Id {获取; 设置;}
public string 名称{获取; set ;}
}





我需要通过代码添加,

另一个属性如Sch_Id。



谢谢。

解决方案

你做不到。唯一的方法是使用 System.Reflection.Emit 使用您的类型发出整个程序集,但此功能很难使用;它设计用于不同的目的。



我可以告诉你正确的方法,但不确定你是否可以使用它。



您应该创建一个通用数据模型来模拟具有任何可想象属性的对象,而不是使用真实属性。 Thiins被称为元数据。您可以对元类型的概念进行建模,此类型的实例将描述哪些属性存在。另一种类型应该模拟此元类型的实例。这种类型的对象(称为元实例)将具有元类型中描述的元属性,但具有它们的单个值。



在简单的情况下,让我们描述你想拥有一些可变数量的属性的情况,但每个属性只是字符串类型。这可以用名称列表来表示,每个名称代表元属性名称。因此,元类型将只有名称列表。表示此元类型实例的类型将引用元类型和适当数量的属性。这些属性可以实现为 System.Collections.Generic.Dictionary< string,string> ,表示属性名称的字典字符串,以及值 - 相应的值属性。 (这就是Javascript属性和数组的工作方式;这就是Javascript如何作为松散类型语言工作,但我正在解释严格打字的元建模。)因为你的元实例应该实现严格的元 - 键入模型(记住,你保持对元类型的引用),字典大小匹配元类型的列表大小(属性列表)。字典中的键是冗余,它们是快速O(1)搜索属性所必需的。但是,您可以根据相应属性的索引以其他方式实现它。



它需要一些工作和思考,但它可以给你系统具有在运行时可修改的此类元属性的数量和名称,因为您可以在运行时始终创建不同的元类型实例。



-SA


你不能那样做。

如果你可能已经注意到,Class是一个模板,那个允许我们从中实例化对象。



不允许我们坚持某事。它只是一个Signature或者我说的模板。

借助这个模板,你可以创建它的实例,调用它的方法,和/或可以使用它的静态变量/函数。但是你仍然无法保存数据。

好​​吧,你当然可以使用Class在某处存储数据,即文本文件,数据库等。



因此解决您的问题的方法是,创建一个类,它可以具有您需要的某些属性,即Id,Name等。

如果您没有使用任何数据存储,那么您可以将这些数据存储在列表< t>< / t> 数组等,并使用它。



赞,

列表< Student> studs =  new 列表< Students>(); 
studs.Add( new 学生{Id = 1 ,名称= AAA});
studs.Add( new 学生{Id = 2 ,姓名= BBB});
studs.Add( new 学生{Id = 3 ,名称= CCC});



如果你想获得学生的数据,其中Id = 1,

学生s = studs.Find(s = >  s.Id ==  1 ); 
Console.Write(s.Name);



相反,我建议你去数据库。 :)



-KR


这是不可能的,但如果你想添加一些可以在运行时初始化的属性你可以这样做



  class  stud 
{
public string ID { get ; set ;}
public string 名称{获取; 设置;}

public string _newVar;

public string newVar
{
get { return _newVar; }
}

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

// 然后你可以初始化newVar在任何地方
stud studObj = new stud();
stud.SetValueFornewVar( Hello World);


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

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



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

Thanks.

解决方案

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


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,

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,

Student s = studs.Find(s => s.Id == 1);
Console.Write(s.Name);


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

-KR


This is not possible yet but if you want to add some property that can initialize at run time you can do like

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");


这篇关于如何以动态方式向模型添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆