C# Passing a class type as a parameter

22,040

Solution 1

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);
        }
    }
}

Solution 2

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()
{

    //..
}

Solution 3

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.

Solution 4

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();
    // ...
}
Share:
22,040
apacay
Author by

apacay

C# Developer, PM, Software architect, Table RPG player(?)

Updated on July 09, 2022

Comments

  • apacay
    apacay almost 2 years

    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 MyFooClass's name or reference I want to receive it from outside. I'll instantiate them inside my adapter code.

    For example:

        public void FillWsvcStructs(DataSet ds, ClassType Baz)
        {
            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)
                {
                    ///From HERE!
                    IElemRequest req = new (Baz)(); // I don't know how to do it here
                    ///To HERE!
    
                    req.Id = string.Empty;
                    req.Type = string.Empty;
                    req.Num = string.Empty;
                    req.Message = string.Empty;
                    req.Trkorr = ar[1];
                    TRequest.Add(req);
                }
            }
        }
    
  • apacay
    apacay almost 12 years
    What if there are more than 1 interface typed elems to instantiate? public void FillWsvcStructs<TBaz, TBar, TQux> How should I declare it?
  • apacay
    apacay almost 12 years
    What if there are more than 1 interface typed elems to instantiate? public void FillWsvcStructs<TBaz, TBar, TQux> How should I declare it?
  • apacay
    apacay almost 12 years
    What if there are more than 1 interface typed elems to instantiate? public void FillWsvcStructs<TBaz, TBar, TQux> How should I declare it?
  • apacay
    apacay almost 12 years
    What if there are more than 1 interface typed elems to instantiate? public void FillWsvcStructs<TBaz, TBar, TQux> How should I declare it?
  • apacay
    apacay almost 12 years
    What if there are more than 1 interface typed elems to instantiate? public void FillWsvcStructs<TBaz, TBar, TQux> How should I declare it?
  • Jesse C. Slicer
    Jesse C. Slicer almost 12 years
    You got it chief! public void FillWsvcStructs<TBaz, TBar, TQux>(DataSet ds) where TBaz: IElemRequest, new() where TBar: TElemRequest, new() where Qux : IElemRequest, new()
  • FishBasketGordo
    FishBasketGordo almost 12 years
    You can add as many type constraints as you have type parameters. See my edit.
  • Mikael
    Mikael almost 12 years
    FillWsvcStructs<T, U, V>(DataSet ds) where T : IElemRequest, new() where U : IElemRequest, new() where V : IElemRequest, new() {}