C# Passing a class type as a parameter

2019-03-21 12:40发布

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

标签: c# oop interface
6条回答
迷人小祖宗
2楼-- · 2019-03-21 13:21

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();
    // ...
}
查看更多
叛逆
3楼-- · 2019-03-21 13:22

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

    //..
}
查看更多
我只想做你的唯一
4楼-- · 2019-03-21 13: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.

查看更多
别忘想泡老子
5楼-- · 2019-03-21 13:23

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);
        }
    }
}
查看更多
倾城 Initia
6楼-- · 2019-03-21 13:24

The method:

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

Calling the method:

FillWsvcStructs<Bez>(ds);
查看更多
迷人小祖宗
7楼-- · 2019-03-21 13:32

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.

查看更多
登录 后发表回答