C # Passing class type as parameter

Is this for C #? Passing a class type as a parameter

I have a class adapter that implements an interface. I want to populate the array structure with instances of MyFooClass, where the name or link of MyFooClass I want to get from the outside. I will create them inside the adapter code.

For instance:

    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);
            }
        }
    }
+5
source share
6 answers

Generics and their limitations 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);
        }
    }
}
+6
source

This is probably best solved with 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 several types that you need to access / instantiate, the declaration follows the same rules (which can be easily compiled from msdn):

public void FillWsvcStructs<T, U, V>() where T : IElemRequest, new() 
                                       where U : IFoo, new()
                                       where V : IBar, new()
{

    //..
}
+6

, generics.

:

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

}

new() T , IElemRequest , IElemRequest.

+4

:

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

( "where..." ) , IElementRequest .

, Baz, :

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

:

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

, , :

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();
    // ...
}
+3

, Activator.CreateInstance.

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

If the type that Bazrepresents has a constructor that accepts parameters, the complexity of this will increase (since you will have to use Reflection or dynamic calls to make it work). If it Bazdoes not represent a type that inherits from IElemRequest, you will get an ugly runtime error.

0
source

Method:

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

Method call:

FillWsvcStructs<Bez>(ds);
0
source

All Articles