Question

I have this code that I am currently adapting to use nested entity (entity framework). I have an entity which contains 2 property which are 2 children entities.

The first step was to read the metadata on both classes, starting from the first classes, building up a list of properties. This is completed. Now I need to iterate over my object to find the good property to do the DataBinding.

This is what I currently have :

variables example :

datasource = namespace.A
propriete = {System.Nullable`1[System.Int32] count} 
propriete.DeclaringType {Name = "prop2" FullName = "Namespace.Metadata.prop2"}

code :

if (this.datasource != null)
{
    var y = (T)this.datasource;

    var propList = typeof(T).GetProperties();
    if (propList.ToList().Contains(propriete))
    {
        TextBox.Text = DataBinder.Eval(y, propriete.Name).ToString();
    }
    else
    {
        TextBox.Text = ":( need child-support!";
    }
}

My main problem is that my object type is unknown till runtime (Type T) so I have no idea on how to find my field.

Quick model :

Class A {
  public B prop1;
  public C prop2;
}

Class B {
   int count;
   string name;
}

Class C {
   int count;
   string name;
}

A.prop1.count = 1;
A.prop1.name = "a";
A.prop2.count = 2;
A.prop2.name = "b";

Right now, my property name are all unique (more specific than count/name), but I expect them to be the same (count/name) at some point.

propriete will probably have to "filter" with DeclaringType/ReflectedType for non-unique name.

A brute-force solution considering unique name, although not elegant, might be accepted.

Extra problem : propriete use another partial class contains in the metadata namespace while datasource use the main class.

(... And if you are curious as to what this system does : It builds a html table (with .net controls) based on an entity based on this metadata.entity dataAttribute.)

Was it helpful?

Solution 2

Here's what I ended up with :

if (this.datasource != null)
{
    var y = (T)this.datasource;

    var propList = typeof(T).GetProperties();
    //try to assign the prop but it might be on a child-prop.
    try 
    {           
        retour = DataBinder.Eval(y, propriete.Name).ToString();
    }
    catch 
    {
        foreach (PropertyInfo prop in propList)
        {
            if ((prop.PropertyType).FullName.Contains("Env."))
            {
                var childPropList = prop.PropertyType.GetProperties();
                foreach (PropertyInfo childProp in childPropList)
                {
                    if (((System.Reflection.MemberInfo)(childProp)).Name == propriete.Name)
                    {
                        var x = DataBinder.GetPropertyValue(y, prop.Name);
                        retour = DataBinder.Eval(x, propriete.Name).ToString();
                    }
                }
            }
        }
    }
}

For now it works, but it might not be versatile enough in a near future.

OTHER TIPS

You can get to the type information by calling GetType() on an instance. So in your case it would be:

var propList = this.datasource.GetType().GetProperties()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top