2

我需要传递某些类型的属性选择(每次一种类型),假设这是我的类型:

public class Product {

    [PrimaryKey]
    public long Id { get; set; }
    [DisplayName("Name")]
    public string Title { get; set; }
    [Foreignkey(Schema = "Products", Table = "MajorCategory", Column = "Id")]
    [DisplayName("MCat")]
    public string MajorCategory { get; set; }
    [Foreignkey(Schema = "Products", Table = "Category", Column = "Id")]
    [DisplayName("Cat")]
    public string Category { get; set; }
    public long CategoryId { get; set; }
    [BoolAsRadio()]
    public bool IsScanAllowed { get; set; }
}

所以我需要一种方法将这种类型的属性列表传递给其他类型(目标类型),并使用属性名称和属性,我不需要值,类似于以下伪代码:

List<Property> propertyList = new List<Property>();
propertyList.Add(Product.Id);
PropertyList.Add(Product.Title);
TargetType target = new TargetType();
target.Properties = propertyList;


public class TargetType  {

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

   GetAttributes() {

      foreach(Property item in Properties){

         Console.WriteLine(item.Name)

         //Get Attributes
      }
   }


}

有什么方法可以传递Product.Id并使用它的名称和属性吗?我不确定,但也许PropertyInfo可以提供帮助,我认为可以传递对象列表,但在这种情况下我不能使用属性和名称,你有什么建议来处理这个问题?或类似的东西?如果我完全错了,我该如何实施呢?

4

5 回答 5

1

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;
   }
}
于 2012-04-18T07:58:41.797 回答
1

有趣的是,我只是在回答一个类似的问题,或者至少我认为是这样。

看起来您正在尝试将两种类型的属性合并为一种?你需要一个 ExpandoObject:

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

有关嵌套合并的实现,请参见:

动态/扩展对象的 C# 深度/嵌套/递归合并

基本上,您需要一个属性的键控列表来开始。以下代码将对任何 .NET 对象执行此操作:

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

之后,这取决于您想要实现的确切目标 - 对象的真实副本,与另一个对象合并,或者只是维护列表。

于 2012-04-18T07:50:53.247 回答
0

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

于 2012-04-18T07:58:55.973 回答
0

您可以使用PropertyInfo,List<PropertyInfo>作为您的类型的列表TargetType .Properties。要获取属性,您可以使用Reflection.

targetType.Properties = product.GetType().GetProperties().ToList();
于 2012-04-18T07:53:58.660 回答
-1

typeof(Product).GetProperties()会给你所有(公共)属性作为PropertyInfo[].

另请参阅MSDN

于 2012-04-18T07:50:14.593 回答