Reflection on generics, could anyone confirm/deny a bug?

  • Thread starter Wiktor Zychla [C# MVP]
  • Start date
W

Wiktor Zychla [C# MVP]

Could anyone confirm/deny that following is a bug (or at least an
"unexpected behaviour")?
If this is not a bug, I would be glad for a short explanation or a
workaround.

Issue:

A generic class, Base, with a constraint on the generic parameter.
A generic class, RelTable, with a constraint on two generic parameters.
A reflection code that just enumerates types, methods and methods'
parameters works perfectly.

Now, add a non-generic class that inherits from Base. Recompile and you get
a reflection exception:

GenericArguments[1], 'TValue', on 'GenericsProblem.RelTable`2[TKey,TValue]'
violates the constraint of type parameter 'TValue'

at System.Signature._GetSignature(SignatureStruct& signature, Void*
pCorSig,
Int32 cCorSig, IntPtr fieldHandle, IntPtr methodHandle, IntPtr
declaringTypeHand
le)
at System.Signature.GetSignature(SignatureStruct& signature, Void*
pCorSig, I
nt32 cCorSig, RuntimeFieldHandle fieldHandle, RuntimeMethodHandle
methodHandle,
RuntimeTypeHandle declaringTypeHandle)
at System.Signature..ctor(RuntimeMethodHandle methodHandle,
RuntimeTypeHandle
declaringTypeHandle)
at System.Reflection.RuntimeMethodInfo.get_Signature()
at System.Reflection.RuntimeMethodInfo.get_ReturnType()
at System.Reflection.RuntimeMethodInfo.ToString()

I admit that I completely do not understand how the exception message is
connected with actual issue (I would rather expect that do report problems
with the FIRST generic argument, since it is "missing" explicitely in
inherited class).

Thanks for any feedback,
Wiktor Zychla

actual code:

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace GenericsProblem
{
class Program
{
static void Main( string[] args )
{
try
{
foreach ( Type t in
Assembly.GetExecutingAssembly().GetTypes() )
{
Console.WriteLine( t.ToString() );
foreach ( MethodInfo mi in t.GetMethods(
BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public ) )
{
Console.WriteLine( mi.ToString() );

foreach ( ParameterInfo pi in mi.GetParameters() )
Console.WriteLine( pi.ToString() );
}
}

Console.ReadLine();
}
catch ( Exception ex )
{
Console.WriteLine( ex.Message );
Console.ReadLine();
}
}
}

class Base<TKey>
where TKey : IComparable
{
public void Method<TValue>( RelTable<TKey, TValue> Table )
where TValue : Base<TKey>, new()
{
}
}

// uncomment this one to get an exception on reflecting Concrete.Method
//class Concrete : Base<int>
//{
//}

class RelTable<TKey, TValue>
where TKey : IComparable
where TValue : Base<TKey>, new()
{
}
}
 
V

Vadym Stetsyak

Hello, Wiktor!

Strange, indeed,

if you remove the constraint on TValue from RelTable, e.g. everything works

class RelTable<TKey, TValue>
where TKey : IComparable
{
}

Also it is interestring why on compilation time there are no errors, only when performing reflection on Concrete type
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
J

Jon Skeet [C# MVP]

Wiktor Zychla said:
Could anyone confirm/deny that following is a bug (or at least an
"unexpected behaviour")?
If this is not a bug, I would be glad for a short explanation or a
workaround.

It certainly looks like a problem to me. Very odd indeed...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top