C# Passing a class type as a parameter

后端 未结 6 1641
清酒与你
清酒与你 2021-02-10 00:49

This for C#? Passing Class type as a parameter

I have a class adapter that implements an Interface. I want to fill an array structure with MyFooClass instances where M

6条回答
  •  一生所求
    2021-02-10 01:22

    You probably want to use Activator.CreateInstance.

    IElemRequest req = (IElemRequest) Activator.CreateInstance(Baz);

    If the type that Baz represents has a constructor that takes parameters, the complexity of that will grow (as you'll have to use Reflection or dynamic calls to make it work). If Baz doesn't represent a type that inherits from IElemRequest, you will get an ugly runtime error.

提交回复
热议问题