tags:

views:

249

answers:

7
+1  Q: 

Type Casting

Hey guys,

I want to pass in the Type of an class to a function, and the class object to a generic function.

I need to be able to cast to that Type (of class) so i can access the class's methods.

Something Like:

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

would implementing an interface for these classes and then casting to that interface work? if so, could someone give an example?

I've been googling for the past few hours, and I've never had to do this before.

can someone shed some light?

Thanks!

A: 

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

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

Hope that helps!

Zachary Yates
I don't think you could call someContainer.Add() in this way, since the type is not known. You'd most likely need to call this method using Reflection.
Adam Lassek
I was really just trying to use the above example.
Zachary Yates
A: 

Check out System.ComponentModel.TypeConverter

Ben Collins
A: 

Hi,

what you try to do is a dynamic cast. This does not exist in C#.

You could try one of the following:

  • Declare the members you want to use in a base class or interface and cast to that.
  • Use Generics as in the code sample of Zachary Yates:

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

Regards, divo

0xA3
A: 

okay

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
      }
Michael G
A: 

I've implemented an Interface, it does the trick! thank you

interface IMediaContainer
{
    BindingList<MediaContainer> addContainer();
}

then used

((IMediaContainer)formSender).addContainer().Add(mc);
Michael G
+3  A: 

Here's three ways to go about what you're asking:

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);
    }
}
David B
A: 

Try this code. Change the "Class2" to "Class1" in main to see the 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);

    }
}

} `

ferrari fan