C#将类类型作为参数传递

时间:2012-06-05 19:14:34

标签: c# oop interface

这适用于C#? Passing Class type as a parameter

我有一个实现接口的类适配器。我想用MyFooClass实例填充数组结构,其中MyFooClass的名称或引用我想从外部接收它。我将在我的适配器代码中实例化它们。

例如:

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

6 个答案:

答案 0 :(得分:6)

使用泛型可能最好解决了这个问题:

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;
   //..
 }

如果您有多种类型需要能够访问/实例化,则声明遵循相同的规则(可以从msdn轻松收集):

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

    //..
}

答案 1 :(得分:6)

Genericstheir constraints应该做你想做的事,我相信:

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

答案 2 :(得分:4)

我想你想要generics

声明你的方法:

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

}

new()约束要求T拥有公共无参数构造函数,IElemRequest约束确保它实现IElemRequest

答案 3 :(得分:3)

您需要generic

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

答案 4 :(得分:0)

您可能想要使用Activator.CreateInstance

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

如果Baz所代表的类型具有带参数的构造函数,那么它的复杂性将会增加(因为您必须使用Reflection或动态调用才能使其工作)。如果Baz不表示继承自IElemRequest的类型,则会出现丑陋的运行时错误。

答案 5 :(得分:0)

方法:

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

调用方法:

FillWsvcStructs<Bez>(ds);