激活.以类作为参数调用构造函数

本文关键字:参数 调用 构造函数 激活 | 更新日期: 2023-09-27 18:10:51

嗨,我想动态地做以下事情我正在使用我自己的CreateInstance方法,但这已经与Activator进行了测试。调用CreateInstance

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

当我尝试使用激活器时,我得到错误,无法转换系统。RuntimeType to 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

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,它是用作参数的值或标识符的集合,另一个属性是我们正在创建的类型的标识符

激活.以类作为参数调用构造函数

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

中更正了它们
    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()更合适
  • 处理派生类型的参数(可能现在有bug,因为需要区分值类型和类类型)

调用代码

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

我可能无法纠正上面示例中的每个缺陷,我只是使其适用于您的场景,即您调用代码

<<p> 泛型实现/strong>
    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});

此代码还可以空参数

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);

无论如何,这是我对你的方法的重构:

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;
}

注意,你似乎把"形参"answers"实参"的概念搞反了。"参数"是方法中接受值的命名部分。"参数"是你传递的实际值。

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