.NET反思:如何获得性能上的部分类定义 [英] .NET reflection: how to get properties defined on partial class
问题描述
我用的是.NET实体框架。我想从一个EntityObject复制属性到另一个。但System.Type.GetProperties()似乎并没有返回的部分类中定义的属性。
I use the .NET Entity Framework. I want to copy properties from one EntityObject to another. But System.Type.GetProperties() does not seem to return the properties defined on the partial class.
code:
在XXX.edmx /由Visual Studio生成XXX.Designer.cs,我有类MyTable的:
In XXX.edmx/ XXX.Designer.cs generated by Visual Studio, I have class MyTable:
public partial class MyTable: EntityObject{..}
我想添加一些属性MyTable的课,所以我想补充文件XXX.Manual.cs:
I want to add some properties to MyTable class, so I add file XXX.Manual.cs:
public partial class MyTable: EntityObject{
public string myProp{get;set;}
}
但 myTableObj.GetType()。GetProperties中()
不包含myProp!
But myTableObj.GetType().GetProperties()
does not contain myProp!!!
怎样才能使用反射myProp?
How can I get myProp using reflection?
我要评论亚历克斯的答案,但不知道为什么code部分没有格式化。
I want to comment to Alex answer but don't know why the code section is not formated.
是的,这是很奇怪的。 我用这个code从实体复制的属性到另一个OBJ:
Yes, this is very strange. I use this code to copy properties from Entity to another obj:
public static void CopyTo(this EntityObject Entity, EntityObject another){
var Type = Entity.GetType();
foreach (var Property in Type.GetProperties()){
...
Property.SetValue(another, Property.GetValue(Entity, null), null);
}
}
//in some other place:
myTableObj.CopyTo(anotherTableObj);
中职高myTableObj和放大器; anotherTableObj的类型的MyTable的。
Of couse myTableObj & anotherTableObj is of type MyTable.
在调试到CopyTo方法,VS表明实体放大器;另一种类型是MyTable的&放大器;我可以看到Entity.myProp,another.myProp
When debug into the CopyTo method, VS show that Entity & another is of type MyTable & I can see Entity.myProp, another.myProp
但物业增值的foreach语句根本就没有循环myProp财产!
But the Property var in foreach statement simply don't loop to myProp property!
不好意思。在code以上(CopyTo方法)是从<副本href="http://stackoverflow.com/questions/2185155/cloning-data-on-entity-framework/5560756#5560756">diamandiev's回答另一个问题
Sorry. The code above (CopyTo method) is copy from diamandiev's answer for another question
但他的code是错误的:中断语句都必须由替换继续:D
But his code is wrong: The "break" statement must be replace by "continue" :D
推荐答案
首先局部类是多么源$ C $ c是分裂。它不影响已编译组件
First of all partial classes is just how source code is split. It does not affect the compiled assembly.
有可能是你没有看到 myProp
属性,因为 myTableObj
的类型是不是的MyTable
。
It is likely that you don't see myProp
property because myTableObj
is not of type MyTable
.
试试这个:
var property = typeof(MyTable).GetProperty("myProp");
刚查:
EntityObject x = new MyTable();
var property1 = typeof(MyTable).GetProperty("myProp");
var property2 = x.GetType().GetProperty("myProp");
两个 property1
和 property2
返回的属性。
想你的code,它经过小改款的工作:
Tried your code, it worked after small modification:
public static void CopyTo(EntityObject fromEntity, EntityObject toEntity)
{
foreach (var property in fromEntity.GetType().GetProperties())
{
if (property.GetSetMethod() == null)
continue;
var value = property.GetValue(fromEntity, null);
property.SetValue(toEntity, value, null);
}
}
这篇关于.NET反思:如何获得性能上的部分类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!