Activator.CreateInstance使用class作为参数调用构造函数

时间:2014-06-28 22:02:37

标签: c# reflection activator

您好我正在尝试动态执行以下操作 我正在使用自己的CreateInstance方法,但已经使用Activator.CreateInstance进行了测试

IPEndPoint newObject = new IPEndPoint(IPAddress.Any, 80);

当我尝试使用激活器时出现错误,无法将System.RuntimeType转换为IPAddress

    public static object CreateInstance(Type context, object[] Params)
    {
        List<Type> argTypes = new List<Type>();

        foreach (object Param in Params)
            argTypes.Add(GetType(Param));
        ConstructorInfo[] Types = context.GetConstructors();
        foreach (ConstructorInfo node in Types)
        {
            ParameterInfo[] Args = node.GetParameters();
            if (Params.Length == Args.Length)
            {
                bool[] cond = new bool[Params.Length];
                for (int i = 0; i < Params.Length; i++)
                    if (argTypes[i] == Args[i].ParameterType) cond[i] = true;
                if (cond[0] == true & cond[1] == true)
                    return node.Invoke(Params);
            }
        }
        return null;
    }

这就是Params在数组中的样子 [0] {Name =“IPAddress”FullName =“System.Net.IPAddress”} [1] 80

这是调用代码,prob之前应该提供它,所以你知道我正在尝试做什么,因为你可以看到它解析代表类的字符串值,这就是为什么我不能使用typeof或typeconstraints。 / p>

private object CreateInstance(ObjectCreationExpression Exp)
{
    object context = GetContext(Exp.Identifier); //Gets the class type
    Type t = (Type)context;
    object[] Params = GetParams(Exp.ArgumentList).ToArray();
    object newObject = Activator.CreateInstance(t, Params);
    return newObject;
}

public static object GetContext(string classname)
{

    return ParseNamespace("System.dll", classname);
}

private static object ParseNamespace(string Namespace, string classname) //Looks up class in System.dll
{
    string DotNetPath = ToolLocationHelper.GetPathToDotNetFramework(TargetDotNetFrameworkVersion.VersionLatest);
    Assembly Asm = Assembly.LoadFile(DotNetPath + @"\" + Namespace);
    Type[] Types = Asm.GetExportedTypes();
    foreach (Type Node in Types)
    {
        if (Node.Name == classname)
            return Node;
    }
    return null;
}

private List<object> GetParams(NodeCollection<ArgumentNode> Params)
{
    List<object> Arguments = new List<object>();
    foreach (ArgumentNode node in Params)
    {

        if (node.Expression is MemberAccessExpression)
        {
            MemberAccessExpression exp = (MemberAccessExpression)node.Expression;
            Type value = (Type)GetContext(exp);
            string name = DirectCast<IdentifierExpression>(exp.Right).Identifier;
            if (value.IsEnum)
            {
                string[] names = DirectCast<Type>(value).GetEnumNames();
                Array item = DirectCast<Type>(value).GetEnumValues();
                Arguments.Add(item.GetValue(names.ToList().IndexOf(name)));
            }
            else
            {
                Type item = value.GetMember(name)[0].ReflectedType;
                Arguments.Add(item);
            }
        }
        else
            Arguments.Add((Int32)ParseType(node.Expression));
    }
    return Arguments;
}

ObjectCreationExpression是一个自定义类,包含用于创建新实例的已解析源代码,两个主要属性是ArgumentList,它是要用作参数的值或标识符的集合,另一个属性是我们正在创建的类型的标识符

2 个答案:

答案 0 :(得分:0)

你已经编写了一个很好的实现来创建对象实例,但它有一些缺陷。我在下面的代码中更正了它们

    public static object CreateInstance(Type context, params object[] Params) // params keyword for array
    {
        List<Type> argTypes = new List<Type>();

        //used .GetType() method to get the appropriate type
        //Param can be null so handle accordingly
        foreach (object Param in Params)
            argTypes.Add((Param ?? new object()).GetType());
        ConstructorInfo[] Types = context.GetConstructors();
        foreach (ConstructorInfo node in Types)
        {
            ParameterInfo[] Args = node.GetParameters();
            if (Params.Length == Args.Length)
            {
                bool[] cond = new bool[Params.Length];
                //handle derived types
                for (int i = 0; i < Params.Length; i++)
                    if (Args[i].ParameterType.IsAssignableFrom(argTypes[i])) cond[i] = true;
                if (cond[0] && cond[1])
                    return node.Invoke(Params);
            }
        }
        return null;
    }
  • 参数不是数组
  • Param.GetType()更合适
  • 处理派生类型的参数(此时可能有错误,因为值类型和类类型需要区分)

调用代码

IPEndPoint newObject = (IPEndPoint)CreateInstance(typeof(IPEndPoint), IPAddress.Any, 80);

注意我可能无法纠正上面示例中的每个漏洞,我只是让它适用于您的场景,即您调用代码

泛型实施

    public static T CreateInstance<T>(params object[] Params) where T : class // params keyword for array
    {
        List<Type> argTypes = new List<Type>();

        //used .GetType() method to get the appropriate type
        //Param can be null so handle accordingly
        foreach (object Param in Params)
            argTypes.Add((Param ?? new object()).GetType());
        ConstructorInfo[] Types = typeof(T).GetConstructors();
        foreach (ConstructorInfo node in Types)
        {
            ParameterInfo[] Args = node.GetParameters();
            if (Params.Length == Args.Length)
            {
                bool[] cond = new bool[Params.Length];
                //handle derived types
                for (int i = 0; i < Params.Length; i++)
                    if (Args[i].ParameterType.IsAssignableFrom(argTypes[i])) cond[i] = true;
                if (cond[0] && cond[1])
                    return (T)node.Invoke(Params);
            }
        }
        return default(T);
    }

调用代码

IPEndPoint newObject = CreateInstance<IPEndPoint>(IPAddress.Any, 80);

完全动态的对象构建

    public static object CreateInstance(Type pContext, object[] Params)
    {
        List<Type> argTypes = new List<Type>();

        //used .GetType() method to get the appropriate type
        //Param can be null so handle accordingly
        if (Params != null)
            foreach (object Param in Params)
            {
                if (Param != null)
                    argTypes.Add(Param.GetType());
                else
                    argTypes.Add(null);
            }

        ConstructorInfo[] Types = pContext.GetConstructors();
        foreach (ConstructorInfo node in Types)
        {
            ParameterInfo[] Args = node.GetParameters();
            // Params can be null for default constructors so use argTypes
            if (argTypes.Count == Args.Length)
            {
                bool areTypesCompatible = true;
                for (int i = 0; i < Params.Length; i++)
                {
                    if (argTypes[i] == null)
                    {
                        if (Args[i].ParameterType.IsValueType)
                        {
                            //fill the defaults for value type if not supplied
                            Params[i] = CreateInstance(Args[i].ParameterType, null);
                            argTypes[i] = Params[i].GetType();
                        }
                        else
                        {
                            argTypes[i] = Args[i].ParameterType;
                        }
                    }
                    if (!Args[i].ParameterType.IsAssignableFrom(argTypes[i]))
                    {
                        areTypesCompatible = false;
                        break;
                    }
                }
                if (areTypesCompatible)
                    return node.Invoke(Params);
            }
        }

        //delegate type to Activator.CreateInstance if unable to find a suitable constructor
        return Activator.CreateInstance(pContext);
    }

调用代码

IPEndPoint newObject = (IPEndPoint)CreateInstance(typeof(IPEndPoint), new object[] { IPAddress.Any, 80});

此代码也可以为null参数

例如

IPEndPoint newObject = (IPEndPoint)CreateInstance(typeof(IPEndPoint), new object[] { IPAddress.Any, null});

我对它进行了简化,并且还为默认构造函数处理了空参数。以及其他一些检查

因此即使您可以construct value types

,此更改也会使其完全动态化

例如

int obj = (int)CreateInstance(typeof(int), null);

案例示例

object context = GetContext(Exp.Identifier);
Type t = (Type)context;
object[] Params = GetParams(Exp.ArgumentList).ToArray();
//use the above defined method and it will work as expected
object newObject = CreateInstance(t, Params);

答案 1 :(得分:0)

对于它的价值,这是我对你的方法的重构:

public static object CreateInstance(Type pContext, params object[] pArguments) {
   var constructors = pContext.GetConstructors();

   foreach (var constructor in constructors) {
      var parameters = constructor.GetParameters();
      if (parameters.Length != pArguments.Length)
         continue;

      // assumed you wanted a matching constructor
      // not just one that matches the first two types
      bool fail = false;
      for (int x = 0; x < parameters.Length && !fail; x++)
         if (!parameters[x].ParameterType.IsInstanceOfType(pArguments[x]))
            fail = true;

      if (!fail)
         return constructor.Invoke(pArguments);
   }
   return null;
}

请注意,您似乎有“参数”和“参数”的概念向后。 “参数”是接受值的方法的命名部分。 “参数”是您传递的实际值。

除此之外,听起来你的问题更多地与你传递的值有关,而不是方法的实现。

相关问题