470,594 Members | 1,491 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

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

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
2 3922
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
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.

Similar topics

9 posts views Thread by Derek Hart | last post: by
6 posts views Thread by Joanna Carter \(TeamB\) | last post: by
7 posts views Thread by Jim Robertson | last post: by
2 posts views Thread by Marc | last post: by
1 post views Thread by Rathish P S | last post: by
23 posts views Thread by Luc Vaillant | last post: by
1 post views Thread by uttara | last post: by
reply views Thread by Konrad Kaczanowski | last post: by
7 posts views Thread by =?Utf-8?B?TXJOb2JvZHk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.