Pass Property to the Method in C#

醉酒当歌 提交于 2019-12-06 11:35:15
Mel Padden

Funny, I was just answering a similar question, or at least I think it is.

It looks like you're trying to concatenate the properties of two types into one? You need an ExpandoObject:

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject%28v=vs.100%29.aspx

For an implementation of a nested merge, see this:

C# deep/nested/recursive merge of dynamic/expando objects

Basically, you want a keyed list of properties, to start from. The following code will do that for any .NET object:

var props = object.GetType().GetProperties().ToDictionary<PropertyInfo, string>(prop => prop.Name);

And after that it depends on what precisely it is you want to achieve - a true copy of the object, merge with another, or just maintaining the list.

You can make use of reflection in .NET here:

List<PropertyInfo> propertyList = new List<PropertyInfo>();
Type productType = typeof (Product);

propertyList.Add(productType.GetProperty("Id"));
propertyList.Add(productType.GetProperty("Title"));
TargetType target = new TargetType();
target.Properties = propertyList;

public class TargetType  {

   public List<PropertyInfo> Properties { get; set;}

   List<object> GetAttributes()
   {
       List<object> attributes = new List<object>();

       foreach(PropertyInfo item in Properties)
       {
           Console.WriteLine(item.Name);
           attributes.AddRange(item.GetCustomAttributes(true));
       }

       return attributes;
   }
}

You can use a list of PropertyInfo, List<PropertyInfo> as the type of your TargetType .Properties. To get the properties you can try it like this using Reflection.

targetType.Properties = product.GetType().GetProperties().ToList();

You can build list of properties using expression trees, e.g. you can make something like this:


var propertiesListBuilder = new PropertiesListBuilder<Product>();
propertiesListBuilder
    .AddProperty(_ => _.Id)
    .AddProperty(_ => _.Title);

var target = new TargetType();
target.Properties = propertiesListBuilder.Properties;

The only concern here is performance, i.e. it might be not good idea to recreate such property lists over and over again, most probably they should be cached. At the same time you'll get intellisense, compiler checks and refactoring support for your property lists.

Below is a sample implementation of this stuff.


static class PropertyInfoProvider<T>
{
    public static PropertyInfo GetPropertyInfo<TProperty>(Expression<Func<T, TProperty>> expression)
    {
        var memberExpression = (MemberExpression)expression.Body;

        return (PropertyInfo)memberExpression.Member;
    }
}

class PropertiesListBuilder<T>
{
    public IEnumerable<PropertyInfo> Properties
    {
        get
        {
            return this.properties;
        }
    }

    public PropertiesListBuilder<T> AddProperty<TProperty>(
        Expression<Func<T, TProperty>> expression)
    {
        var info = PropertyInfoProvider<T>.GetPropertyInfo(expression);
        this.properties.Add(info);

        return this;
    }

    private List<PropertyInfo> properties = new List<PropertyInfo>();
}

typeof(Product).GetProperties() would give you all (public) properties as PropertyInfo[].

See also MSDN.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!