【问题标题】:Loading a Class through Generics通过泛型加载类
【发布时间】:2011-08-08 20:44:47
【问题描述】:

我对此感到困惑。但这是我想要做的。这是类结构:

public class Order
{
    public Int32 orderID { get; set; }
    public Int32 CustomerID { get; set; }
    public Int32 ProductID { get; set; }
    public string OrderName { get; set; }
    public Product ProductDetails { get; set; }
    public Customer CustomerDetails { get; set; }

}

public class Product
{
    public Int32 ProductID { get; set; }
    public string Name { get; set; }
}

public class Customer
{
    public Int32 CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

我正在构建一个通用方法,它采用 XML 内容并创建和加载传递类型的对象实例。我让产品和客户正常工作。但是当涉及到订单时,它会变得混乱。

    public static T LoadObject<T>(string Contents) where T : new()
    {
        T obj = new T();
        foreach (PropertyInfo property in typeof (T).GetProperties())
        {
            if (property.PropertyType.IsValueType || property.PropertyType.IsPrimitive)
            {
                object propValue = Convert.ChangeType(GetValue(property.PropertyType, Contents),
                                                      property.PropertyType);
                property.SetValue(obj, propValue, null);
            }
            else
            {
                //Type typeArgument = property.PropertyType;
                //Type genericClass = t
                //object propValue = LoadObject<> (dr);
                //property.SetValue(obj, propValue, null);
            }
        }
        return obj;
    }

如何递归调用,以便 Order 加载客户和产品?

【问题讨论】:

  • 你不能使用 XmlSerializer 来完成这个吗?
  • 这些类都相当小,所以我不太明白你为什么需要这样一个通用的解决方案(YAGNI 原则......)。我只是为它们中的每一个实现一些“FromXml”方法,并使其成为接口方法或其他东西。考虑到反射实际上非常缓慢 - 当您要处理数千个订单时,它会很明显。一遍又一遍地检索相同的PropertyInfos 成本很高。
  • 这只是我想出的一个例子来更好地解释这个问题。实际上,这些不是类。而且输入也不必是 XML。

标签: c# .net reflection generics


【解决方案1】:

您可以使用反射进行递归调用:

public void Test<T>()
{
    var methodInfo = GetType().GetMethod("Test");
    methodInfo = methodInfo.MakeGenericMethod(typeof(int));
    methodInfo.Invoke(this, new object[0]);
}

【讨论】:

    【解决方案2】:

    问题是你不能调用LoadObject&lt;T&gt;,而Tproperty.PropertyType,因为你不能在编译时使用参数未知的泛型。我自己也遇到过这样的问题,我曾经问过一个与你的问题有点相关的问题:Generic static class - retrieving object type in run-time(相关 - 如果我理解你的话,那就是。否则忽略它)。

    【讨论】:

      【解决方案3】:

      通常,在这种情况下,您会在 Order 类中创建 Customer 和 Product 实例,并在 order 类上使用 Load 方法来填充 Order,然后填充 Customer 实例和 Product 实例。

      public class Order
      {  
         public void LoadOrder(int orderID)
         {
             //load order
      
             //load customer from order
      
             //load product from order
         }
      
         public Customer Cust
         {
            get; set;
         }
         public Product Prod
         {
            get; set;
         }
      }
      

      我知道你的问题有点不同,但希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-01
        相关资源
        最近更新 更多