Vil*_*men 0 c# constructor activator
基本上我要做的是创建一个可以使用大量创建对象的类
Activator.CreateInstance(Type type, params object[] args)
Run Code Online (Sandbox Code Playgroud)
我需要将所有对象蓝图传递给一个名为ObjectMap的类的构造函数.它需要是一个类型和参数的对.如果允许解决方案,它也可以是另一个类而不是构造函数的方法.
有点像
new ObjectMap([Type, somevalue, somevalue, somevalue], [Type, somevalue], [Type] ...)
Run Code Online (Sandbox Code Playgroud)
要么
Resources.AddObjectMap([Type, somevalue, somevalue, somevalue], [Type, somevalue], [Type] ...)
Run Code Online (Sandbox Code Playgroud)
我不知道如何制作它,以便您可以传递可变数量的对与可变数量的参数(甚至0).哎呀,我甚至很难解释这个问题.问我任何你不清楚的事情= S.
Gr.Viller
我建议你将"type和args"封装成一个特定的类型......然后你可以使用它的params数组.例如:
// TODO: Find a better name :)
public class TypeBlueprint
{
public Type Type { get; set; }
public List<object> Arguments { get; set; }
public TypeBlueprint()
{
this.Arguments = new List<object>();
}
public TypeBlueprint(Type type, params object[] arguments)
{
this.Type = type;
this.Arguments = arguments.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
public ObjectMap(params TypeBlueprint[] blueprints)
Run Code Online (Sandbox Code Playgroud)
并称之为:
var map = new ObjectMap(new TypeBlueprint(typeof(Foo), "x", "y", "z"),
new TypeBlueprint { Type = typeof(Bar),
Arguments = { 1, 2, 3 } });
Run Code Online (Sandbox Code Playgroud)
这表明使用构造函数参数和对象初始值设定项来指定类型和参数.使用最适合您的方式.
| 归档时间: |
|
| 查看次数: |
5709 次 |
| 最近记录: |