【问题标题】:How can I dynamically initialize items from the class constructor?如何从类构造函数动态初始化项目?
【发布时间】:2016-04-04 14:22:09
【问题描述】:

在我的类构造函数中,我正在初始化许多 List[DropDownItem] 列表.....我不想再在构造函数中列出所有这些,我希望它们能够自动初始化。

我有以下内容,可以识别我需要定位的类型,但我还不能让它越过球门线。无法弄清楚如何执行“obj = new List(); 部分。

private void initializeDropdownItemLists(Type T)
{
    var props = typeof(UserEditModel).GetProperties();

    foreach (var p in props)
    {
        var type = p.PropertyType;
        if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
        {
            var itemType = type.GetGenericArguments()[0];

            if (itemType == typeof(DropdownItem))
            {
                p.SetValue(T, new List<DropdownItem>());
            }
        }

    }
}


public class UserEditModel
{
    public UserEditModel()
    {
        selectedUser = new UserBusinessModel();
        currentUser = new UserBusinessModel();

        //  I want to replace this
        ddl1 = new List<DropdownItem>();
        ddl2 = new List<DropdownItem>();
        ddl3 = new List<DropdownItem>();
        ddl4 = new List<DropdownItem>();
        ddl5 = new List<DropdownItem>();
        ddl6 = new List<DropdownItem>();
        ddl7 = new List<DropdownItem>();
        ddl8 = new List<DropdownItem>();
        ddl9 = new List<DropdownItem>();
        ddl10 = new List<DropdownItem>();

        //  with this
        this.initializeDropdownItemLists();

        //  or better yet this, ..... pass the class type so then any similar class can use it
        someHelper.initializeDropdownItemLists(typeof(UserEditModel));
    }

    public UserBusinessModel currentUser { get; set; }
    public UserBusinessModel selectedUser { get; set; }

    //  other properties omitted for brevity ...
}

【问题讨论】:

  • 我看不到你提到的obj = new List() 部分...
  • 您需要将UserEditModel 传递给方法,或者在方法的开头写var userEditModel = new UserEditModel()。然后,你会写:p.SetValue(userEditModel, new List&lt;DropdownItem&gt;());
  • 我根据您的评论进行了一些更改,但出现错误:对象与目标类型不匹配。我一定做错了什么。这有点超出我的深度。
  • 为什么不给支持字段一个初始化器,为什么要使用反射代码,你以后必须仔细阅读才能看到它的作用?
  • Lasse,我不明白你的意思。

标签: c# class types constructor


【解决方案1】:

使用以下代码:

p.SetValue(someUserEditModelObject, new List<DropdownItem>());

其中 someUserEditModelObject 是传递给 initializeDropdownItemLists 方法的对象。

编辑: 通用助手代码:

    public class Helper
    {
        public void InitializeDropdownItemLists<T>(T model)
        {
            var props = typeof (T).GetProperties();

            foreach (var p in props)
            {
                var type = p.PropertyType;
                if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof (List<>))
                {
                    var itemType = type.GetGenericArguments()[0];

                    if (itemType == typeof (DropdownItem))
                    {
                        p.SetValue(model, new List<DropdownItem>());
                    }
                }

            }
        }
    }

【讨论】:

  • 你不应该通过初始化方法 typeof(UserEditModel) 传递 "this": someHelper.initializeDropdownItemLists(this); 并将 private void initializeDropdownItemLists(Type T) 更改为 private void initializeDropdownItemLists(UserEditModel T)
  • 另一种情况是使其通用,然后您必须声明方法如下:private void initializeDropdownItemLists&lt;T&gt;(T model) 并更改 p.SetValue(T, new List());到p.SetValue(model, new List&lt;DropdownItem&gt;());
猜你喜欢
  • 1970-01-01
  • 2013-05-23
  • 1970-01-01
  • 2017-01-18
  • 2015-08-08
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多