使用动态生成的类型进行变量声明和赋值 [英] Using dynamically generated type for variable declaration and assignment
问题描述
在此问题之后,有可能像这样动态创建类型和实例形式:
Following this question, it is possible to create a type and an instance form it dynamically like this:
var type = typeof(AnimalContext<>).MakeGenericType(a.GetType());
var a_Context = Activator.CreateInstance(type);
太好了.
但是,我要做的不是使用 Activator.CreateInstance(type);
创建确定类型的实例,而是使用动态创建的类型来声明变量,而为我动态创建的类型分配一个实例.
However, what I want to do instead of creating an instance of a determined type with Activator.CreateInstance(type);
is to use a dynamic created type to declare a variable, and assign an instance to my dynamically created type.
种类:
myDynamicallyCreatedType variableName = figuredTypeInstace;
但是我不能使用创建的type( var type = typeof(AnimalContext<>).MakeGenericType(a.GetType());
进行声明.这可能吗?
But I cannot use the created type(var type = typeof(AnimalContext<>).MakeGenericType(a.GetType());
for declarations. Is that possible?
修改:
提出需求的简短方案说明.我需要调用一个将从"topManager"调用的方法,该topManager包含实现相同基本接口 ImplementedMethod
.
Short scenario explanation where the need came up. I need to call a method that will be called from a "topManager", this topManager holds the respective instance of the types type1Manager
and type2Manager
that implement the same base interface IMyInterface
method ImplementedMethod
.
我想做的事情,可以通过 if
s来解决,就像这样:
What I am trying to do, could be solved with if
s, like this:
private int HandleInstance(Type itemType, //other args) {
if (itemType == Type.type1) {
topManagerInstance.manager1Instance.ImplementedMethod(//args):
}
elseif (itemType == Type.type2) {
topManagerInstance.manager2Instance.ImplementedMethod(//args):
}
...not meaningful code
{
但是,我想知道是否可以解决避免 if
的类型,例如(用于识别问题关键的大写字母,而不是大喊大叫):
But, I was wondering if it could be solved handling types to avoid the if
s, like (caps used to spot the key of the question out, not to shout):
private int HandleInstance(Type itemType, //other args) {
Type managerType = itemType == Type.type1 ? typeof(manager1Type) :
typeof(manager2Type);
Type[] managerTypeArray = { managerType, typeof(int) };
var myDynamicallyCreatedType = typeof(IMyInterface<,>).MakeGenericType(managerTypeArray);
//KEY OF THE QUESTION. THIS IS WHAT I AM ASKING FOR
//assign created variable to dynamic created type to call respective method
myDynamicallyCreatedType variableName = topManagerInstance.type1Manager;
//base type. any type1ManagerType or type2ManagerType to be assigned, as
//they inherit from the same IMyInterface<,>, and the type created is
//generic
variableName.ImplementedMethod(//args):
}
推荐答案
似乎您只是想将枚举值映射到特定实现中的函数调用.一种方法是让工厂类使用用作地图的Dictionary来处理它.例如:
It seems like you're just looking to map an enum value to a function call in a specific implementation. One way to do that is to have a factory class that handles it with a Dictionary used as a map. For example:
进行如下设置:
// The enum you use for mapping
public enum Thing
{
Foo,
Bar
}
// The various implementations...
public interface ISomeInterface
{
void SomeMethod();
}
public class Foo : ISomeInterface
{
public void SomeMethod() => Console.WriteLine("Foo method!");
}
public class Bar : ISomeInterface
{
public void SomeMethod() => Console.WriteLine("Bar method!");
}
现在您可以拥有一个看起来像这样的工厂:
Now you could have a factory that looks like this:
public class MyThingFactory
{
private Dictionary<Thing, ISomeInterface> _registry;
public MyThingFactory()
{
_registry = new Dictionary<Thing, ISomeInterface>
{
{Thing.Foo, new Foo()},
{Thing.Bar, new Bar()},
};
}
public void RunMethod(Thing thing)
{
if(!_registry.TryGetValue(thing, out var item))
{
throw new ArgumentOutOfRangeException(nameof(thing));
}
item.SomeMethod();
}
}
并这样称呼它:
// You may want to make this static for performance reasons since you won't recreate
// the dictionary every time
var factory = new MyThingFactory();
factory.RunMethod(Thing.Foo);
factory.RunMethod(Thing.Bar);
//Output:
//Foo method!
//Bar method!
这篇关于使用动态生成的类型进行变量声明和赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!