dnatoli
dnatoli

Reputation: 7012

Retrieving Generic Argument Types

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?

Upvotes: 0

Views: 132

Answers (4)

John Saunders
John Saunders

Reputation: 161773

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

Upvotes: 1

Matt Hamilton
Matt Hamilton

Reputation: 204139

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?

Upvotes: 0

Henry Gao
Henry Gao

Reputation: 4936

is typeof working?

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

Upvotes: 0

Yuriy Faktorovich
Yuriy Faktorovich

Reputation: 68687

public class MyClass<T>
{
    public void MyMethod()
    {
        if (typeof(T).GetGenericArguments().First() == typeof(int))
        {
            Console.WriteLine("Hello World!!!");
        }
    }
}

Upvotes: 0

Related Questions