Click here to Skip to main content
15,886,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

i have this classes in my project

C#
public class A{
    public A(B b, C c) {
            this.b = b;
            this.c = c;
        }
    B b;
    C c;
}

public class B{
    public B(DataRow row) {
            if (row.Table.Columns.Contains("UserName"))
                this.UserName = row["UserName"].ToString();

            if (row.Table.Columns.Contains("Password"))
                this.Password = row["Password"].ToString();
        }
    public string UserName { get; set; }
    public string Password { get; set; }

    public object MyToObject(){
    }
}

public class C{
    public C(DataRow row) {
            if (row.Table.Columns.Contains("UserName"))
                this.UserName = row["UserName"].ToString();

            if (row.Table.Columns.Contains("Password"))
                this.Password = row["Password"].ToString();
        }
    public string UserName { get; set; }
    public string Password { get; set; }
}


then, i want to take an object as output from MyToObject function that is declared in Class A,
that the output object contains all of the properties of b and c, like this:

C#
output object = {b.UserName, b.PassWord, c.UserName, c.PassWord}
Posted

1 solution

This will give you a list of properties:

C#
PropertyInfo[] propsB = typeof(b).GetProperties(BindingFlags.Public | BindingFlags.Instance);
PropertyInfo[] propsC = typeof(c).GetProperties(BindingFlags.Public | BindingFlags.Instance);


Then to put them into a list...

C#
List<string> propNames = new List<string>();

foreach (PropertyInfo p in propsB)
{
    propNames.Add("b." + p.Name);
}

foreach (PropertyInfo p in propsC)
{
    propNames.Add("c." + p.Name);
}


And there you have it, a list called propNames with all the public instance properties of class b and class c.

[Edit]

If you want to create a dynamic object from the properties, that's something much more difficult and requires using the framework Emit functions and low level MSIL.

What is the ultimate goal here? Are you trying to create a dynamic object based on static code? Why would you want to create an object with the properties of the two other classes if the classes are well known?
 
Share this answer
 
v3
Comments
shajarian_lover 19-Jun-13 16:20pm    
can you explain more about how can i create dynamic object?
Ron Beyer 19-Jun-13 16:35pm    
You can try DynamicObject or if you want to generate true dynamic types: True Dynamic Types
shajarian_lover 19-Jun-13 16:41pm    
how can i get the value of properties?
Ron Beyer 19-Jun-13 16:57pm    
p.GetValue(<instanceofb>, null), replacing the <instanceofb> with the actual instance of the class b that you want to get the property value of. For example, c objc = new c(); then find the property you want in the list, and get its value like p.GetValue(objc, null)
Sergey Alexandrovich Kryukov 19-Jun-13 16:56pm    
Correct, with one exclusion: you should not hard-code "b." and "c."; for this purpose, Type.Name of Type.FullName should be used. Hard-coding of such names strongly defeats the purpose of reflection.
(I voted 4 this time.)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900