2008-11-25 69 views
1

我想将类的tType传递给函数,并将类对象传递给泛型函数。如何实现一个接口来处理类型转换类?

我需要能够转换为类(类),所以我可以访问类的方法。

喜欢的东西:

void GenericFunction(Object obj, Type type) 
{ 
    (type)obj.someContainer.Add(1); 
} 

会实现一个接口,这些类,然后浇铸到该接口的工作? 如果是这样,有人可以举个例子吗?

我一直在谷歌搜索过去几个小时,我从来没有这样做过。

有人可以发光吗?

回答

3

这里有三种方式去了解你的要求:

public interface ICanReport 
{ void Report(); } 

public class SomeThing : ICanReport 
{ 
    public void Report() 
    { Console.WriteLine("I'm SomeThing"); } 
} 

public class SomeOtherThing : ICanReport 
{ 
    public void Report() 
    { Console.WriteLine("I'm SomeOtherThing"); } 
} 

public class TestThings 
{ 
    //#1 use safe downcasting 
    public void TheMethod(object x) 
    { 
     ICanReport y = x as ICanReport; 
     if (y != null) 
      y.Report(); 
    } 

    //#2 use generics 
    // 100% safe, but a little complex 
    public void AnotherMethod<T>(T x) where T : ICanReport 
    { 
     x.Report(); 
    } 

    //#3 use an interface as the parameter type. 
    // simple and safe 
    public void LastMethod(ICanReport x) 
    { 
     x.Report(); 
    } 

    //sample calls 
    public void Test1() 
    { 
     SomeThing a = new SomeThing(); 
     SomeOtherThing b = new SomeOtherThing(); 
     TheMethod(a); 
     TheMethod(b); 
     AnotherMethod(a); 
     AnotherMethod(b); 
     LastMethod(a); 
     LastMethod(b); 
    } 
} 
0

你可以在你的方法调用中使用泛型吗?例如:

void GenericFunction<T>(Object obj) 
    where T : class { 
    obj.someContainer.Add(1) as T; 
} 

希望帮助!

+0

我不认为你可以这样调用someContainer.Add(),因为类型是未知的。你很可能需要使用Reflection来调用这个方法。 – 2008-11-25 19:34:12

0

你试图做的是动态转换。这在C#中不存在。

你可以尝试以下操作之一:

  • 声明你要在基类或接口,并投给使用该成员。
  • 使用泛型作为扎卡里耶茨的代码示例:

    void GenericFunction<T>(Object obj) where T : class 
    { 
        obj.someContainer.Add(1) as T; 
    } 
    
0

好吗

class A { 
    public BindingList<int> addContainers; 
} 

class B { 
    public BindingList<int> addContainers; 
} 

class C { 
    Type type; 
    Object senderObj; 

    C(Object s, Type t) 
    { 
      senderObj = s; 
      type = t; 
    } 

    private void AddBtn_Click(click sender, EventArgs e) 
    { 
      // Depending on the Type passed to the constructor, i need to cast to that type 
      // so that i have access to the classes public addContainer member variable 
     } 
0

试试这个代码。将Class2更改为Class1以查看结果。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 

namespace ReflectionTest 
{ 
    class Class1 
    { 
     public void helloWorld() 
     { 
      Console.WriteLine("Hello World 1"); 
     } 
    } 

    class Class2 
    { 
     public void helloWorld() 
     { 
      Console.WriteLine("Hello World Class 2"); 
     } 
    } 

    class Program 
    { 
     static void callCorrectClass(Object obj, Type type) 
     { 
      ConstructorInfo constructors = type.GetConstructor(System.Type.EmptyTypes); 
      obj = constructors.Invoke(null); 
      MethodInfo helloWorld = type.GetMethod("helloWorld"); 
      helloWorld.Invoke(obj, null); 
     } 
     static void Main(string[] args) 
     { 
      Type type = typeof(Class2); 
      callCorrectClass(new Object(), type); 
     } 
    } 
} 
相关问题