0

所以我有一个方法可以像这样创建一个新对象:

public class Objects<TObject>
{
    public NodeReference<TObject> CreateObject<TObject>(TObject objectType) 
                where TObject: class, new()
            {
                NodeReference<TObject> nodeReference = 0;

                return nodeReference;
            }
}

现在我有其他对象类来定义其他对象类型,例如 Car。

public class Car
{
    public int NumberOfDoors {get; set;}
    public int NumberOfWheels {get; set;}
}

现在假设我得到了一个字符串,我将如何在运行时将此字符串转换为它的类型?

var carObjectReference = CreateObject<//string converted to identify object car dynamically>(//new Car { });

4

1 回答 1

1
public object ReturnValue(string operationName, object returnValue)
{
    Type t = returnValue.GetType();
    return Activator.CreateInstance(t);
}
于 2013-11-18T20:39:49.183 回答