By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,509 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,509 IT Pros & Developers. It's quick & easy.

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

P: n/a
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()
{
}
}

Apr 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Apr 10 '06 #2

P: n/a
Wiktor Zychla [C# MVP] <wz*****@nospm.ii.uni.wroc.pl.nospm> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.