I have a List that contains objects and each of these objects contains a lot of properties of several types, and each of these properties contains subproperties aswell.
I need to get all the properties via Reflection and store them in one PropertyInfo[] ...
Is this even possible with reflection? I really need to do it via reflection...
There is no such thing as "sub properties" - properties are of a certain type, can have values of a certain type (ie. a subclass of the property type), and that type can have properties of its own.
You can use recursion for this:
List<PropertyInfo> properties = new List<PropertyInfo>();
foreach (object obj in myList)
{
properties.AddRange(GetDeepProperties(obj, ...));
}
PropertyInfo[] array = properties.ToArray();
...
IEnumerable<PropertyInfo> GetDeepProperties(object obj, BindingFlags flags)
{
// Get properties of the current object
foreach (PropertyInfo property in obj.GetType().GetProperties(flags))
{
yield return property;
object propertyValue = property.GetValue(obj, null);
if (propertyValue == null)
{
// Property is null, but can still get properties of the PropertyType
foreach (PropertyInfo subProperty in property.PropertyType.GetProperties(flags))
{
yield return subProperty;
}
}
else
{
// Get properties of the value assiged to the property
foreach (PropertyInfo subProperty = GetDeepProperties(propertyValue))
{
yield return subProperty;
}
}
}
}
The above code is just an example:
I have not tried or even compiled it
you'll get a StackOverflowException if somewhere in this "property tree" objects are pointing to eachother
it misses null checks and exception handling (property getters can throw exceptions)
it ignores the existence of indexed properties
I don't know what you want to do with this array - the reference to the object from which each PropertyInfo was created is lost, so you can't get or set their values anymore.
Example:
class Program
{
static void Main(string[] args)
{
PropertyInfo[] result = GetAllPropertyInfos(typeof(Example)).ToArray(); ;
foreach (string property in result.Select(p => string.Format("{0} : {1}",p.Name,p.PropertyType.Name)))
{
Console.WriteLine(property);
}
}
static IEnumerable<PropertyInfo> GetAllPropertyInfos(Type type)
{
List<PropertyInfo> result = new List<PropertyInfo>();
foreach (PropertyInfo propertyInfo in type.GetProperties())
{
result.Add(propertyInfo);
result.AddRange(GetAllPropertyInfos(propertyInfo.PropertyType));
}
return result;
}
}
class Example
{
public AnotherExample AProperty { get; set; }
public int AnotherProperty { get; set; }
}
class AnotherExample
{
public int YetAnotherProperty { get; set; }
}
Output:
AProperty : AnotherExample
YetAnotherProperty : Int32
AnotherProperty : Int32
Related
From the world of PHP I have decided to give C# a go. I've had a search but can't seem to find the answer of how to do the equivalent to this.
$object = new Object();
$vars = get_class_vars(get_class($object));
foreach($vars as $var)
{
doSomething($object->$var);
}
I basically have a List of an object. The object could be one of three different types and will have a set of public properties. I want to be able to get a list of the properties for the object, loop over them and then write them out to a file.
I'm thinking this has something to do with c# reflection but it's all new to me.
Any help would be greatly appreciated.
This should do it:
Type myType = myObject.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = prop.GetValue(myObject, null);
// Do something with propValue
}
void Test(){
var obj = new{a="aaa", b="bbb"};
var val_a = obj.GetValObjDy("a"); //="aaa"
var val_b = obj.GetValObjDy("b"); //="bbb"
}
//create in a static class
static public object GetValObjDy(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
}
Yes, Reflection would be the way to go. First, you would get the Type that represents the type (at runtime) of the instance in the list. You can do this by calling the GetType method on Object. Because it is on the Object class, it's callable by every object in .NET, as all types derive from Object (well, technically, not everything, but that's not important here).
Once you have the Type instance, you can call the GetProperties method to get the PropertyInfo instances which represent the run-time informationa about the properties on the Type.
Note, you can use the overloads of GetProperties to help classify which properties you retrieve.
From there, you would just write the information out to a file.
Your code above, translated, would be:
// The instance, it can be of any type.
object o = <some object>;
// Get the type.
Type type = o.GetType();
// Get all public instance properties.
// Use the override if you want to classify
// which properties to return.
foreach (PropertyInfo info in type.GetProperties())
{
// Do something with the property info.
DoSomething(info);
}
Note that if you want method information or field information, you would have to call the one of the overloads of the GetMethods or GetFields methods respectively.
Also note, it's one thing to list out the members to a file, but you shouldn't use this information to drive logic based on property sets.
Assuming you have control over the implementations of the types, you should derive from a common base class or implement a common interface and make the calls on those (you can use the as or is operator to help determine which base class/interface you are working with at runtime).
However, if you don't control these type definitions and have to drive logic based on pattern matching, then that's fine.
well, in C# it's similar.
Here's one of the simplest examples (only for public properties):
var someObject = new { .../*properties*/... };
var propertyInfos = someObject.GetType().GetProperties();
foreach (PropertyInfo pInfo in propertyInfos)
{
string propertyName = pInfo.Name; //gets the name of the property
doSomething(pInfo.GetValue(someObject,null));
}
One line solution using Linq...
var obj = new {Property1 = 1, Property2 = 2};
var property1 = obj.GetType().GetProperties().First(o => o.Name == "Property1").GetValue(obj , null);
To get specific property value from property name
public class Bike{
public string Name {get;set;}
}
Bike b = new Bike {Name = "MyBike"};
to access property value of Name from string name of property
public object GetPropertyValue(string propertyName)
{
//returns value of property Name
return this.GetType().GetProperty(propertyName).GetValue(this, null);
}
You can use GetType - GetProperties - Linq Foreach:
obj.GetType().GetProperties().ToList().ForEach(p =>{
//p is each PropertyInfo
DoSomething(p);
});
Here's something I use to transform an IEnumerable<T> into a DataTable that contains columns representing T's properties, with one row for each item in the IEnumerable:
public static DataTable ToDataTable<T>(IEnumerable<T> items)
{
var table = CreateDataTableForPropertiesOfType<T>();
PropertyInfo[] piT = typeof(T).GetProperties();
foreach (var item in items)
{
var dr = table.NewRow();
for (int property = 0; property < table.Columns.Count; property++)
{
if (piT[property].CanRead)
{
var value = piT[property].GetValue(item, null);
if (piT[property].PropertyType.IsGenericType)
{
if (value == null)
{
dr[property] = DBNull.Value;
}
else
{
dr[property] = piT[property].GetValue(item, null);
}
}
else
{
dr[property] = piT[property].GetValue(item, null);
}
}
}
table.Rows.Add(dr);
}
return table;
}
public static DataTable CreateDataTableForPropertiesOfType<T>()
{
DataTable dt = new DataTable();
PropertyInfo[] piT = typeof(T).GetProperties();
foreach (PropertyInfo pi in piT)
{
Type propertyType = null;
if (pi.PropertyType.IsGenericType)
{
propertyType = pi.PropertyType.GetGenericArguments()[0];
}
else
{
propertyType = pi.PropertyType;
}
DataColumn dc = new DataColumn(pi.Name, propertyType);
if (pi.CanRead)
{
dt.Columns.Add(dc);
}
}
return dt;
}
This is "somewhat" overcomplicated, but it's actually quite good for seeing what the outcome is, as you can give it a List<T> of, for example:
public class Car
{
string Make { get; set; }
int YearOfManufacture {get; set; }
}
And you'll be returned a DataTable with the structure:
Make (string)
YearOfManufacture (int)
With one row per item in your List<Car>
This example trims all the string properties of an object.
public static void TrimModelProperties(Type type, object obj)
{
var propertyInfoArray = type.GetProperties(
BindingFlags.Public |
BindingFlags.Instance);
foreach (var propertyInfo in propertyInfoArray)
{
var propValue = propertyInfo.GetValue(obj, null);
if (propValue == null)
continue;
if (propValue.GetType().Name == "String")
propertyInfo.SetValue(
obj,
((string)propValue).Trim(),
null);
}
}
I haven't found this to work on, say Application objects. I have however had success with
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string rval = serializer.Serialize(myAppObj);
You can try this:
string[] arr = ((IEnumerable)obj).Cast<object>()
.Select(x => x.ToString())
.ToArray();
Once every array implements IEnumerable interface
public Dictionary<string, string> ToDictionary(object obj)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
Type objectType = obj.GetType();
IList<PropertyInfo> props = new List<PropertyInfo>(objectType.GetProperties());
foreach (PropertyInfo prop in props)
{
object propValue = prop.GetValue(obj, null);
dictionary.Add(prop.Name, propValue.ToString());
}
return dictionary;
}
/// get set value field in object to object new (two object field like )
public static void SetValueObjectToObject (object sourceObj , object resultObj)
{
IList<PropertyInfo> props = new List<PropertyInfo>(sourceObj.GetType().GetProperties());
foreach (PropertyInfo prop in props)
{
try
{
//get value in sourceObj
object propValue = prop.GetValue(sourceObj, null);
//set value in resultObj
PropertyInfo propResult = resultObj.GetType().GetProperty(prop.Name, BindingFlags.Public | BindingFlags.Instance);
if (propResult != null && propResult.CanWrite)
{
propResult.SetValue(resultObj, propValue, null);
}
}
catch (Exception ex)
{
// do something with Ex
}
}
}
I need to access something like strClassname.strPropertyName I will have different values for strClassname and strProperty name in program execution.
Please direct me in right way.
Sounds to me like you are trying to get (or set) the value of a property on an object at runtime. So here's the most basic way to do this:
public static object GetPropertyValue(object instance, string strPropertyName)
{
Type type = instance.GetType();
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(strPropertyName);
return propertyInfo.GetValue(instance, null);
}
... and to set a value:
public static void SetPropertyValue(object instance, string strPropertyName, object newValue)
{
Type type = instance.GetType();
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(strPropertyName);
propertyInfo.SetValue(instance, newValue, null);
}
If you're attempting to get the names of properties of a class, here's a function for that:
public static IEnumerable<string> GetPropertyNames(string className)
{
Type type = Type.GetType(className);
return type.GetProperties().Select(p => p.Name);
}
Say that you have 100 objects, and you want to get the value of the Name property on each of them, here's a function that will do that:
public static IEnumerable<String> GetNames(IEnumerable<Object> objects, string nameProperty = "Name")
{
foreach (var instance in objects)
{
var type = instance.GetType();
var property = type.GetProperty(nameProperty);
yield return property.GetValue(instance, null) as string;
}
}
You can use reflection:
To get names of properties for a specific type use method Type.GetProperĀties. Method returns array of PropertyInfo objects and the property names are available through PropertyInfo.Name property. If you want to get only subset of all properties (e.g. only public static ones) use BindingFlags when calling GetProperties method. You have to specify at least two flags, one from Public/NonPublic and one of Instance/Static flags. If you use GetProperties without a BindingFlags parameter, default flags are Public + NonPublic + Instance.
Following example shows how to get public static properties.
using System.Reflection; // reflection namespace
// get all public static properties of MyClass type
PropertyInfo[] propertyInfos;
propertyInfos = typeof(MyClass).GetProperties(BindingFlags.Public |
BindingFlags.Static);
// sort properties by name
Array.Sort(propertyInfos,
delegate(PropertyInfo propertyInfo1, PropertyInfo propertyInfo2)
{ return propertyInfo1.Name.CompareTo(propertyInfo2.Name); });
// write property names
foreach (PropertyInfo propertyInfo in propertyInfos)
{
Console.WriteLine(propertyInfo.Name);
}
[Source]
if there's a hundred or so classes and you know you want to access a specific property on each and you know every class will be instantiated, you should definitely consider creating an interface holding the property you wish to access ex.
public interface INamed
{
Name { get; }
}
Example usage:
var namedInstances = listOfClasses.Of<INamed>().Cast<INamed>();
foreach(var instance in namedInstances)
{
var name = instance.Name;
}
On the other hand, if you're not planning to instantiate these classes, you could try the following approach instead if the 'Name' property is static or const:
public interface INamed { } //Marker interface
public static class GetNamedHelper
{
private static IEnumerable<Type> GetAssemblyTypes(IEnumerable<Assembly> assemblies)
{
if (assemblies == null) yield break;
foreach (var assembly in assemblies.Where(assembly => assembly != null))
{
IEnumerable<Type> types;
try
{
types = assembly.GetTypes().Where(t => t != null);
}
catch (ReflectionTypeLoadException rtle)
{
types = rtle.Types.Where(t => t != null);
}
foreach (var type in types)
yield return type;
}
}
private static readonly Type namedMarkerInterface = typeof (INamed);
public static IEnumerable<string> GetNames(params Assembly[] assemblies)
{
var types = GetAssemblyTypes(assemblies)
.Where(t => t.GetInterfaces().Any(intf => intf == namedMarkerInterface));
foreach (var type in types)
{
//ex. public static string Name
var prop = type.GetProperty("Name", BindingFlags.Public | BindingFlags.Static);
if (prop == null || !prop.CanRead) continue;
yield return prop.GetValue(null, null) as string;
//ex. public const string Name
var field = type.GetField("Name", BindingFlags.Public);
if (field == null || !field.IsStatic) continue;
yield return field.GetValue(null) as string;
}
}
}
Eitherway, you need to know which classes to check and for what.
Well I'm tring to get class members values from a dynamically casted class but I'm unable to find its child class members values.
Right now I'm getting TotalWeight members property, but I also want to get child member property of AnotherClass like AnotherClass.child. How can I get those members?
string ClassName="something";
Type types = Type.GetType(ClassName, false);
var d = from source in types.GetMembers().ToList()
where source.MemberType == MemberTypes.Property
select source;
List<MemberInfo> members = d.Where(memberInfo =>
d.Select(c => c.Name)
.ToList()
.Contains(memberInfo.Name))
.ToList();
PropertyInfo propertyInfo;
object value;
foreach (var memberInfo in members)
{
propertyInfo = typeof(T).GetProperty(memberInfo.Name);
if (myobj.GetType().GetProperty(memberInfo.Name) != null)
{
value = myobj.GetType()
.GetProperty(memberInfo.Name)
.GetValue(myobj, null);
//how to get child members value here?
}
}
//Where class something has member
public class something
{
private decimal _totalWeight;
private Anotherclass _another;
public decimal TotalWeight
{
get
{
return this._totalWeight;
}
set
{
this._totalWeight = value;
}
}
public Anotherclass Another
{
get
{
return this._another;
}
set
{
this._another= value;
}
}
}
You can get type of your properties and after that work with this type as in start of your code.
foreach( var memberInfo in members )
{
propertyInfo = types.GetProperty( memberInfo.Name );
if( propertyInfo != null )
{
Console.WriteLine( propertyInfo.PropertyType );
// type of every property
}
}
I have POCO object MyObject
public class MyModel
{
public MyProperty MyProperty001 { get; set; }
public MyProperty MyProperty002 { get; set; }
MyModel()
{
// New up all the public properties
var properties = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var propertyInfo in properties)
{
//Activator.CreateInstance()
}
}
}
that has hundreds of properties, is it possible using reflection to instantiate these in the constructor? I have the PropertyInfo, but don't know what is the next step.
Thank you,
Stephen
Type of property holds within of PropertyType property of PropertyInfo object, so according to that you can instantiate your objects by calling Activator.CreateInstance(propertyInfo.PropertyType). Than you need to set instance into property of your container object by calling propertyInfo.SetValue(this, instance, null)
Full sample:
foreach (var propertyInfo in properties)
{
var instance = Activator.CreateInstance(propertyInfo.PropertyType);
propertyInfo.SetValue(this, instance, null);
}
Assuming you want to set the property value to a new instance of the according property type, this should be possible the following way:
var instance = propertyInfo.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
propertyInfo.SetValue(this, instance, null);
public class MyModel
{
private MyProperty _MyProperty001 = new MyProperty();
public MyProperty MyProperty001
{
get { return _myProperty001; }
set { _MyProperty001 = value; }
}
}
Use a backing field, no need for reflection.
If they are no parameter constructors, you could do
foreach (var propertyInfo in properties) {
ConstructorInfo ci = propertyInfo.GetType().GetConstructor(new Type[] { propertyInfo.PropertyType() });
propertyInfo.SetValue(this, ci.Invoke(new object[] { }), null);
}
I have a project in which I have to get all the variables from a struct in a different class and add all their names to a combo box in a form. My primary issue at the moment is iterating through the struct in a way that I can add each variable individually to the combo box. I tried doing this:
msgVars vars = new msgVars();
foreach (object obj in vars)
{
comboBox1.Items.Add(GetName(obj));
}
but as you probably know, you can't iterate through a struct that way. Is there any way I can do this without hardcoding the variables in?
Also, for reference, this is the GetName function:
static string GetName<T>(T item) where T : struct
{
var properties = typeof(T).GetProperties();
if (properties.Length == 1)
{
return properties[0].Name;
}
else
{
return null;
}
}
If you are looking for a way to access properties of a struct (or class) at runtime without predefining in code that set of properties, what you probably need to use is reflection.
Here's an example:
struct MyStruct
{
private readonly int m_Age;
private readonly string m_LastName;
private readonly string m_FirstName;
public int Age { get { return m_Age; } }
public string LastName { get { return m_LastName; } }
public string FirstName { get { return m_FirstName; } }
public MyStruct( int age, string last, string first )
{
m_Age = age;
m_LastName = last;
m_FirstName = first;
}
}
class StructReflection
{
public static void Main(string[] args)
{
var theStruct = new MyStruct( 40, "Smith", "John" );
PrintStructProperties( theStruct );
}
public static void PrintStructProperties( MyStruct s )
{
// NOTE: This code will not handle indexer properties...
var publicProperties = s.GetType().GetProperties();
foreach (var prop in publicProperties)
Console.WriteLine( "{0} = {1}", prop.Name, prop.GetValue( s, null ) );
}
}
You could try using Reflection. How about storing the information in a Hashtable.
public Hashtable GetPropertyInfo(Person person)
{
var properties = new Hashtable();
PropertyInfo[] propInfo = person.GetType().GetProperties();
foreach (PropertyInfo prop in propInfo)
{
properties.Add(prop.Name, prop.GetValue(person, null));
}
return properties;
}
Then you could write the information out via:
var person = new Person()
Person.Name = "Test";
Person.Age = 21;
var PropertyInfo = GetPropertyInfo(person);
foreach (string key in PropertyInfo.Keys)
{
Console.WriteLine("{0} = {1}", key, PropertyInfo[key]);
}
A struct is a single entity, not a collection of variables. This means that you can't 'iterate' over its properties. What you need to do is get a collection of the property names and iterate over it. Your GetName function can't do this because it just returns the name of the first property.
To add the property names to a combo all you have to do is:
var vars = new msgVars();
foreach(var name in GetNames(vars))
comboBox1.Items.Add(name);
In fact, getting the property name is so easy that you could get rid of GetNames completley and just write
foreach (var prop in typeof(msgVars).GetProperties())
comboBox1.Items.Add(prop.Name);
There are various ways you can write GetNames to return a collection of names. You can fill a List with the property names although the simplest is to have it return an iterator like this:
public static IEnumerable<string> GetNames<T>(T obj) where T:struct
{
var properties = typeof (T).GetProperties();
foreach (var propertyInfo in properties)
{
yield return propertyInfo.Name;
}
}
Finally, you don't really need to pass an instance of your struct to the method, as you are enumerating the structs property names, not their values. You can rewrite GetNames like this
public static IEnumerable<string> GetNames<T>() where T:struct
{
var properties = typeof (T).GetProperties();
foreach (var propertyInfo in properties)
{
yield return propertyInfo.Name;
}
}
and load the names like this
foreach(var name in GetNames<msgVars>())
comboBox1.Items.Add(name);