.NET反射:如何获取在部分类上定义的属性 [英] .NET reflection: how to get properties defined on partial class

查看:95
本文介绍了.NET反射:如何获取在部分类上定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.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.

代码:

在Visual Studio生成的XXX.edmx / 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?

我想对Alex回复发表评论,但不知道为什么代码段没有形成。

I want to comment to Alex answer but don't know why the code section is not formated.

是的,这很奇怪。
我使用此代码将属性从Entity复制到另一个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);

couse myTableObj& anotherTableObj是MyTable类型。

Of couse myTableObj & anotherTableObj is of type MyTable.

当调试到CopyTo方法时,VS显示Entity&另一个是MyTable&我可以看到Entity.myProp,另一个.myProp

When debug into the CopyTo method, VS show that Entity & another is of type MyTable & I can see Entity.myProp, another.myProp

但是foreach语句中的Property var不会循环到myProp属性!

But the Property var in foreach statement simply don't loop to myProp property!

抱歉。上面的代码(CopyTo方法)是从 diamandiev的另一个问题的答案复制

Sorry. The code above (CopyTo method) is copy from diamandiev's answer for another question

但是他的代码是错误的:break语句必须用continue替换:D

But his code is wrong: The "break" statement must be replace by "continue" :D

推荐答案

首先,部分类只是源代码的分解。它不会影响编译程序集。

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.

尝试这样: / p>

Try this:

var property = typeof(MyTable).GetProperty("myProp");

刚刚检查:

EntityObject x = new MyTable();

var property1 = typeof(MyTable).GetProperty("myProp");
var property2 = x.GetType().GetProperty("myProp");

property1 property2 返回属性。

尝试你的代码,它在小修改后工作:

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屋!

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