473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4214
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
6
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T...
7
by: Jim Robertson | last post by:
Is it possible to instantiate a generic class when the parametrized type isn't known until runtime? For example, in Indigo you can deploy a web service with something like... ServiceHost<Foo> sh =...
2
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
1
by: Rathish P S | last post by:
Hi friends, I am trying with Reflection and Generics in C#. But I get confused with some of these new features. I am trying to get the type of generic parameter with the...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
0
by: Konrad Kaczanowski | last post by:
Hi all, I'm creating code generator for wrappers of some c# classes. With the introduction of c# 2.0 and generics the following problem arises. When encountering generic types anywhere inside the...
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.