473,405 Members | 2,354 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,405 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 4215
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.