C# 3.0 如何实现处理类型转换类的接口?

C# 3.0 如何实现处理类型转换类的接口?,c#-3.0,casting,C# 3.0,Casting,我想把类的tType传递给函数,把class对象传递给泛型函数 我需要能够强制转换到该类型(类),以便访问该类的方法 比如: void GenericFunction(Object obj, Type type) { (type)obj.someContainer.Add(1); } void GenericFunction<T>(Object obj) where T : class { obj.someContainer.Add(1) as T; } 为这些类

我想把类的tType传递给函数,把class对象传递给泛型函数

我需要能够强制转换到该类型(类),以便访问该类的方法

比如:

void GenericFunction(Object obj, Type type)
{
    (type)obj.someContainer.Add(1);
}
void GenericFunction<T>(Object obj)
  where T : class {
  obj.someContainer.Add(1) as T;
}
为这些类实现一个接口,然后将其转换为该接口是否有效? 如果是,有人能举个例子吗

在过去的几个小时里,我一直在谷歌上搜索,我以前从来没有这样做过


有人能解释一下吗?

你能在方法调用中使用泛型吗?比如:

void GenericFunction(Object obj, Type type)
{
    (type)obj.someContainer.Add(1);
}
void GenericFunction<T>(Object obj)
  where T : class {
  obj.someContainer.Add(1) as T;
}
void GenericFunction(对象对象对象)
T:在哪里上课{
对象someContainer.添加(1)作为T;
}

希望有帮助

查看

您尝试做的是动态播放。这在C#中不存在

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

  • 声明要在基类或接口中使用的成员,并强制转换为该成员
  • 在Zachary Yates的代码示例中使用泛型:

    void GenericFunction<T>(Object obj) where T : class 
    {
      obj.someContainer.Add(1) as T;
    }
    
    void GenericFunction(objectobj),其中T:class
    {
    对象someContainer.添加(1)作为T;
    }
    
    • 好的

      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
            }
      
      A类{
      公共绑定列表容器;
      }
      B类{
      公共绑定列表容器;
      }
      C类{
      类型;
      对象senderObj;
      C(对象s,类型t)
      {
      senderObj=s;
      类型=t;
      }
      私有void AddBtn_单击(单击发件人、事件参数e)
      {
      //根据传递给构造函数的类型,我需要强制转换为该类型
      //这样我就可以访问classes public addContainer成员变量
      }
      
      以下三种方法可以解答您的问题:

      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);
          }
      }
      
      公共接口报告
      {void Report();}
      公开课:我的报告
      {
      公开无效报告()
      {Console.WriteLine(“我是某样东西”);}
      }
      公共类:我的报告
      {
      公开无效报告()
      {Console.WriteLine(“我是另一个人”);}
      }
      公共类测试对象
      {
      //#1使用安全向下投射
      公共无效方法(对象x)
      {
      ICANLeport y=x作为ICANLeport;
      如果(y!=null)
      y、 报告();
      }
      //#2.使用泛型
      //100%安全,但有点复杂
      公共无效无热法(T x),其中T:ICANTREPORT
      {
      x、 报告();
      }
      //#3使用接口作为参数类型。
      //简单安全
      公共方法(ICANx报告)
      {
      x、 报告();
      }
      //电话样本
      公共void Test1()
      {
      某物a=新的某物();
      SomeOtherThing b=新的SomeOtherThing();
      方法(a);
      方法(b);
      无热法(a);
      无热法(b);
      方法(a);
      方法(b);
      }
      }
      
      尝试此代码。将main中的
      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);
              }
          }
      }
      

      我不认为您可以用这种方式调用someContainer.Add(),因为类型未知。您很可能需要使用反射调用此方法。我只是尝试使用上面的示例。