English 中文(简体)
从类型中创建通用变量 - 如何创建? 或者使用活动器。 具有 {} 属性的 CreateInstance () 而不是参数 ()?
原标题:Creating generic variables from a type - How? Or use Activator.CreateInstance() with properties { } instead of parameters ( )?

目前我使用通用的常规来制造一些动态方法, 比如创建对象和填充属性中的值。

是否有办法“动态地”在不知道该类型的情况下创造通用的?例如:

List<String> = new List<String>()

这是一种预定的方式,但

List<(object.GetType())> = new List<(object.GetType()>()

没用,但能吗?

这行不通(是否有一种模拟方法有效? )

    public T CreateObject<T>(Hashtable values)
    {
        // If it has parameterless constructor (I check this beforehand)
        T obj = (T)Activator.CreateInstance(typeof(T));

        foreach (System.Reflection.PropertyInfo p in typeof(T).GetProperties())
        {
            // Specifically this doesn t work
            var propertyValue = (p.PropertyType)values[p.Name];
            // Should work if T2 is generic
            // var propertyValue = (T2)values[p.Name];

            obj.GetType().GetProperty(p.Name).SetValue(obj, propertyValue, null);
        }
    }

所以,简言之:如何从“类型”中取一个“类型”并从中创建一个对象而不使用“通用”呢?我迄今只在方法中使用了“通用”一词,但能否在变量中使用同样的方式?我必须在方法之前定义“通用(T) ”, 所以在“ 生成” 变量之前,我可以对变量做同样的定义吗?

... 或如何使用“ 活动器” 来创建带有属性而非参数的对象。 如您在这里所做的 :

/ 带有参数值的参数值

Test t = new Test("Argument1", Argument2);

/ 有属性的 /

Test t = new Test { Argument1 = "Hello", Argument2 = 123 };
最佳回答

您可以使用 makeGenericType :

Type openListType = typeof(List<>);
Type genericListType = openListType.MakeGenericType(obj.GetType());
object instance = Activator.CreateInstance(genericListType);
问题回答

您可以使用 < a href=>" http://msdn.microsoft.com/ en-us/library/ system.type. makegenerictype.aspx" rel = "noreferr" >MakeGenericType 的方法获取特定类型参数的通用类型 :

var myObjListType = typeof(List<>).MakeGenericType(myObject.GetType());
var myObj = Activator.CreateInstance(myObjListType);
// MyObj will be an Object variable whose instance is a List<type of myObject>

当您使用对象初始化器时,它只是使用默认构建器(无参数),然后在构建对象后设定单个属性。

上述代码已接近 - 但 var 将无法在此工作, 因为它只是一个编译时间型的推断。 既然您已经在使用反射, 您可以使用 System.Object :

object propertyValue = values[p.Name];

SetValue 呼叫将使用 System.Object





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...