C# Passing a class type as a parameter

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

    The method:

    public void FillWsvcStructs<T>(DataSet ds) where T : IElemRequest, new() {
        ...
        IElemRequest req = new T();
        ...
    }
    

    Calling the method:

    FillWsvcStructs<Bez>(ds);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-10 01:27

    I think you want generics.

    Declare your method like this:

    public void FillWsvcStructs<T>(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.

    0 讨论(0)
  • 2021-02-10 01:30

    Generics and their constraints should do what you want, I believe:

    public void FillWsvcStructs<ClassType>(DataSet ds) where ClassType : IElemRequest, new()
    {
        IFoo.Request = ds.DataTable[0].Request;
        IFoo.Modulebq = string.Empty;
        IFoo.Clasific = string.Empty;
        IFoo.Asignadoa = ds.DataTable[0].Referer;
        IFoo.Solicita = "Jean Paul Goitier";
    
    
        // Go with sub-Elems (Also "Interfaceated")
        foreach (DataSet.DataTableRow ar in ds.DataTable[1])
        {
            if ((int) ar.StatusOT != (int)Props.StatusOT.NotOT)
            {
                IElemRequest req = new ClassType();
    
                req.Id = string.Empty;
                req.Type = string.Empty;
                req.Num = string.Empty;
                req.Message = string.Empty;
                req.Trkorr = ar[1];
                TRequest.Add(req);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-10 01:40

    That's probably best solved by using generics:

    public void FillWsvcStructs<T>(DataSet ds) where T : IElemRequest, new()
    {
       //..
       //new() constraint enables using default constructor     
       IElemRequest req = new T(); 
       //IElemRequest constraint enables using interface properties
       req.Id = string.Empty;
       //..
     }
    

    If you have multiple types that you need to be able to access/instantiate, the declaration follows the same rules (as may easily be gathered from msdn):

    public void FillWsvcStructs<T, U, V>() where T : IElemRequest, new() 
                                           where U : IFoo, new()
                                           where V : IBar, new()
    {
    
        //..
    }
    
    0 讨论(0)
  • 2021-02-10 01:43

    You need a generic:

    public void FillWsvcStructs<TBaz>(DataSet ds) where TBaz : IElementRequest, new()
    {
        // ...
        IElementRequest req = new TBaz();
        // ...
    }
    

    The generic constraint ("where...") enforces that the type you pass in implements the IElementRequest interface and that it has a parameter-less constructor.

    Assuming you had a class Baz similar to this:

    public class Baz : IElementRequest
    {
        public Baz()
        {
        }
    }
    

    You would invoke this method like so:

    DataSet ds = new DataSet();
    FillWsvcStructs<Baz>(ds);
    

    Addendum

    Multiple, different, generic type parameters can each have there own type constraint:

    public void FillWsvcStructs<TFoo, TBar, TBaz>(DataSet ds) 
        where TFoo : IFoo, new()
        where TBar : IBar, new()
        where TBaz : IElementRequest, new()
    {
        // ...
        IFoo foo = new TFoo();
        IBar bar = new TBar();
        IElementRequest req = new TBaz();
        // ...
    }
    
    0 讨论(0)
提交回复
热议问题