How can I implement an interface to handle casting classes?

I want to pass the tType of the class to the function and the object of the class to the generic function.

I need to be able to apply this type (of a class) so I can access the methods of the class.

Something like:

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

      

Will there be implemented an interface for these classes and then casting for that interface to work? If so, can anyone provide an example?

I've been working at Googling for the past few hours and I've never had to do this before.

Can someone shed some light?

+1


source to share


6 answers


Here are three ways to do what you ask:



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

      

+3


source


Can you use generics in a method call? Something like:

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

      



Hope it helps!

0


source


0


source


What you are trying to do is a dynamic cast. This doesn't exist in C #.

You can try one of the following:

  • Declare the elements you want to use in the base class or interface and add to them.
  • Use Generics as in the Zachary Yates example code:

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

0


source


fine

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


source


Try this code. Change Class2

to Class1

in main to see results.

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

      

0


source