C# Passing a class type as a parameter

后端 未结 6 1644
清酒与你
清酒与你 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:27

    I think you want generics.

    Declare your method like this:

    public void FillWsvcStructs(DataSet ds)
        where T : IElemRequest, new()
    {
        //You can then do
        IElemRequest req = new T();
    
    }
    

    The new() constraint requires T to have a public parameterless constructor, and the IElemRequest constraint ensures it implements IElemRequest.

提交回复
热议问题