5

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);
            }
        }
    }
Community
  • 1
  • 1
apacay
  • 1,702
  • 5
  • 19
  • 41
  • There was an answer that stated to use `.ToThisType()` and `.ToTheOtherType()` having thesame class controlling what it would get..., because in some point the class you are using knows what should she get there. It was quite useful too. a pity I cant Upvote since it was deleted. However now u know that solution also exists. Thanks, _unsung hero_! – apacay Jun 06 '12 at 17:48

6 Answers6

8

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);
        }
    }
}
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • What if there are more than 1 interface typed elems to instantiate? `public void FillWsvcStructs` How should I declare it? – apacay Jun 05 '12 at 20:27
  • 1
    You got it chief! `public void FillWsvcStructs(DataSet ds) where TBaz: IElemRequest, new() where TBar: TElemRequest, new() where Qux : IElemRequest, new()` – Jesse C. Slicer Jun 05 '12 at 20:31
6

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

    //..
}
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
  • What if there are more than 1 interface typed elems to instantiate? `public void FillWsvcStructs` How should I declare it? – apacay Jun 05 '12 at 20:27
4

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.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • What if there are more than 1 interface typed elems to instantiate? `public void FillWsvcStructs` How should I declare it? – apacay Jun 05 '12 at 20:27
3

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();
    // ...
}
Community
  • 1
  • 1
FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
0

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.

48klocs
  • 6,073
  • 3
  • 27
  • 34
0

The method:

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

Calling the method:

FillWsvcStructs<Bez>(ds);
Mikael
  • 500
  • 2
  • 15
  • What if there are more than 1 interface typed elems to instantiate? `public void FillWsvcStructs` How should I declare it? – apacay Jun 05 '12 at 20:27
  • FillWsvcStructs(DataSet ds) where T : IElemRequest, new() where U : IElemRequest, new() where V : IElemRequest, new() {} – Mikael Jun 06 '12 at 11:43