4

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:

In XXX.edmx/ XXX.Designer.cs generated by Visual Studio, I have class MyTable:

public partial class MyTable: EntityObject{..}

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

But myTableObj.GetType().GetProperties() does not contain myProp!!!

How can I get myProp using reflection?

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

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

Of couse myTableObj & anotherTableObj is of type MyTable.

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

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

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

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

Community
  • 1
  • 1

2 Answers2

6

First of all partial classes is just how source code is split. It does not affect the compiled assembly.

It is likely that you don't see myProp property because myTableObj is not of type MyTable.

Try this:

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

[EDIT]

Just checked:

EntityObject x = new MyTable();

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

Both property1 and property2 returned the property.

[EDIT]

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);
    }
}
Alex Aza
  • 76,499
  • 26
  • 155
  • 134
  • did you mean "is **not** of type"? If I do `myTableObj.GetType().GetProperties()` as the OP did I get myProp (when myTableObj is of type MyTable). Does EF generate proxies? – TrueWill Jun 24 '11 at 03:41
  • @TrueWill - yes, missed **not**. Thank you. – Alex Aza Jun 24 '11 at 03:51
  • @Thành Bùi Việt - I tried your code, it worked after small modification. – Alex Aza Jun 24 '11 at 04:34
  • @Thành Bùi Việt - are you sure that your partial classes is in the same namespace? If the namespace is different they will be two separate classes, not one. – Alex Aza Jun 24 '11 at 04:38
  • I must be missing something because your last code block with a "small modification" barely resembles the code in the question. There are so many modifications that I can't tell what actually fixed the problem. – Zach Mierzejewski Jan 15 '18 at 20:56
2

Albeit late, my issue lay in the fact that Visual Studio generated the following code:

public System.Windows.Forms.ListView myListView;

However, before my ListView would show up in the GetProperties method, I had to do the following:

public System.Windows.Forms.ListView myListView { get; set; }

Hope this may help out someone else.

Wotuu
  • 828
  • 1
  • 8
  • 18
  • 1
    You are correct in the diagnosis, but missed the solution. The solution is not to add get and set, but to use GetFields instead of GetProperties. – Ghasan غسان Nov 16 '17 at 06:55