How to obtain attributes declared on incomplete classes using.NET reflection

.net c# entity-framework linq reflection

Question

The.NET Entity Framework is what I use. I want to transfer properties between two EntityObjects. However, it seems that the properties declared on the partial class are not returned by System.Type.GetProperties().

Code:

I have class MyTable in XXX.edmx/XXX.Designer.cs produced by Visual Studio.

public partial class MyTable: EntityObject{..}

I create file XXX.Manual because I want to add certain attributes to the MyTable class. cs:

public partial class MyTable: EntityObject{
    public string myProp{get;set;}
}

But myTableObj.GetType().GetProperties() does not include myProp at all!

How can I get myProp through reflection?

[EDIT] Alex's response is what I want to remark on, however I'm not sure why the code area is not formatted.

This is definitely pretty unusual. To copy properties from one entity to another object, I use the following code:

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

Naturally, myTableObj and anotherTableObj are objects of the type MyTable.

VS indicates that Entity and another are of type MyTable and I can see Entity while debugging the CopyTo function. another is myProp. myProp

However, the Property var in the foreach expression does not loop to the myProp property at all!

[EDIT] Sorry. The code above is a clone of Diamandiev's response to a different query's CopyTo method.

Yet his code is incorrect: The word "continue" must be used in lieu of the word "break":

1
4
5/23/2017 11:48:32 AM

Accepted Answer

First off, source code is simply divided into partial classes. The assembled code is unaffected.

It's possible that you missmyProp property sincemyTableObj not the kindMyTable .

Do this:

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

[EDIT]

Just verified:

EntityObject x = new MyTable();

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

Both property1 and property2 delivered the item back.

[EDIT]

I tested your code, and with a tiny adjustment it worked:

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);
    }
}
6
6/24/2011 4:34:14 AM

Popular Answer

My problem, even though it was late, was that Visual Studio produced the following code:

public System.Windows.Forms.ListView myListView;

However, I had to carry out the following before my ListView would appear in the GetProperties method:

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

Hope someone else can benefit from this.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow