views:

51

answers:

4

I am using VB.Net. I have an object class called clsA(of T as clsB). Depending on what T is, I want to do different things. Let's say that clsC and clsD both inherit clsB and therefore can be used for T.

If I have an instance of clsA(of clsC), how can I get the inside type (i.e. clsC) using reflection? Solutions in C#.Net would be fine if you don't know the VB.Net answer.

+1  A: 

Reflection is a .NET technology - it's not specific to either VB.NET or C#.

Given an object, o, use

o.GetType().GetGenericArguments()(0)

However, it's probably a bad idea to explicitly vary your behavior based on the type. If you need to do different things based on class "A" vs. class "B", then you should use virtual methods or properties, and override them in the derived types:

Public MustInherit Class BaseClass
    Public MustOverride Function OneMethodTwoWays() As Integer

    Public MustOverride ReadOnly Property OnePropertyTwoWays() As Integer

End Class

Public Class DerivedClass1
    Inherits BaseClass


    Public Overrides Function OneMethodTwoWays() As Integer
        Return 1 + 1
    End Function

    Public Overrides ReadOnly Property OnePropertyTwoWays() As Integer
        Get
            Return 1 + 1
        End Get
    End Property
End Class

Public Class DerivedClass2
    Inherits BaseClass

    Public Overrides Function OneMethodTwoWays() As Integer
        Return 2 * 1
    End Function

    Public Overrides ReadOnly Property OnePropertyTwoWays() As Integer
        Get
            Return 2 * 1
        End Get
    End Property
End Class

Public Class MyGeneric(Of T As BaseClass)
    Public Function DoTheyMatch(ByVal a As T, ByVal b As T) As Boolean
        Return a.OneMethodTwoWays() = b.OnePropertyTwoWays
    End Function
End Class
John Saunders
Thanks, exactly what I needed.
link664
A: 
public class MyClass<T>
{
    public void MyMethod()
    {
        if (typeof(T).GetGenericArguments().First() == typeof(int))
        {
            Console.WriteLine("Hello World!!!");
        }
    }
}
Yuriy Faktorovich
A: 

is typeof working?

Type t = typeof(Apple); string className = t.ToString();

Henry Gao
A: 

I'd love to see a concrete example of what you're trying to do. Whenever you find yourself taking different actions depending on an object's type, it's a sign that you need to revisit your object model.

Here's a really contrived example:

public void Talk(Animal a)
{
    if (a is Dog) 
    {
        Console.WriteLine("Woof!"); 
    }
    else if (a is Cat)
    { 
        Console.WriteLine("Meow!"); 
    }
}

You're far better off adding a virtual "Talk" method to Animal and overriding it in Dog and Cat, so that your method becomes:

public void Talk(Animal a)
{
    a.Talk();
}

Can you refactor your code in such a way that the generic class doesn't need to know too much about its parameterized type?

Matt Hamilton
My recent example of this was with a Solr search. It returns an xml object based on configuration. So I have a project which generates a class based on Solr's schema, and fills it from the xml response. Hope that makes sense.
Yuriy Faktorovich
I want to be able to collect values depending on T, and then create a new instance of T. Using your example, I want to declare Object(of Dog) and then set a property called Toy as an instance of Bone. If I declare Object(of Cat), the Toy must be an instance of Mouse. Once this property is set then I will create a new instance of Cat/Dog with the Toy property for the object set. I know that there are heaps of ways to do this, but there are other factors into why I'm going down this route.
link664
@link664 - Fair enough. I just wanted to put this out there so that others can see that there's sometimes a better option.
Matt Hamilton