Differentiating between generic and non-generic version of overloaded method using reflection - c#

I'm having some trouble using reflection to differentiate between a non-generic and a generic method on a generic class. Here's a test case I'm working with:
public class Foo<T>
{
public string Bar( T value ) { return "Called Bar(T)"; }
public string Bar( int value ) { return "Called Bar(int)"; }
public static void CallBar<TR>(Foo<TR> foo)
{
var fooInfo = foo.GetType()
.GetMethods()
.Where(x => !x.IsGenericMethod && // doesn't filter out Bar(T)!
x.Name == "Bar" &&
x.GetParameters().First().ParameterType == typeof(int))
// !Two identical MethodInfo results, how to choose between them?
// Is there a gauranteed canonical ordering? Or is it undefined?
.First();
Console.WriteLine(fooInfo.Invoke(foo, new object[]{ 0 }));
}
}
// prints Bar(T)...
Foo<int>.CallBar( new Foo<int>() );

Unfortunately System.Reflection doesn't provide a good way to correlate a method on a constructed type with the corresponding method on the generic type definition from which it was constructed. There are two solutions I know of, neither one is perfect:
Solution #1: static TypeBuilder.GetMethod. There's a static version of GetMethod on TypeBuilder that accepts a generic constructed type and a MethodInfo for a method on a generic type definition, and returns the
corresponding method on the specified generic type. In this example, calling TypeBuilder.GetMethod(Foo<int>, Foo<T>.Bar(T)) will give you Foo<int>.Bar(T-as-int) which you can then use to disambiguate between it and Foo<int>.Bar(int).
(The above example will not compile, naturally; I've used Foo<int> and Foo<T>.Bar(T) to mean the respective Type and MethodInfo objects which, which are easily obtainable but would make the example too complex).
The bad news is that this only works when the generic type definition is a TypeBuilder, i.e. when you're emitting a generic type.
Solution #2: MetadataToken. It's a little known fact that type members retain their MetadataToken in the transition from generic type definitions to generic constructed types. So in your example, Foo<T>.Bar(T) and Foo<int>.Bar(T-as-int) should share the same MetadataToken. That would allow you to do this:
var barWithGenericParameterInfo = typeof(Foo<>).GetMethods()
.Where(mi => mi.Name == "Bar" &&
mi.GetParameters()[0].ParameterType.IsGenericParameter);
var mappedBarInfo = foo.GetType().GetMethods()
.Where(mi => mi.MetadataToken == genericBarInfo.MetadataToken);
(This will not compile either, unless I'm extremely lucky and managed to get it right the first time :) )
The problem with this solution is that MetadataToken wasn't meant for that (probably; the documentation is a little skimpy on that) and it feels like a dirty hack. Nevertheless, it works.

When using Foo<int>, the Bar(T) method is typed as Bar(int), making no distinction between it and the method with an int defined as the parameter.
To get the correct method definition of Bar(T), you can use typeof(Foo<>) instead of typeof(Foo<int>).
This will enable you to tell the difference between the two. Try the following code:
public static void CallBar<TR>(Foo<TR> foo)
{
Func<MethodInfo, bool> match = m => m.Name == "Bar";
Type fooType = typeof(Foo<>);
Console.WriteLine("{0}:", fooType);
MethodInfo[] methods = fooType.GetMethods().Where(match).ToArray();
foreach (MethodInfo mi in methods)
{
Console.WriteLine(mi);
}
Console.WriteLine();
fooType = foo.GetType();
Console.WriteLine("{0}:", fooType);
methods = fooType.GetMethods().Where(match).ToArray();
foreach (MethodInfo mi in methods)
{
Console.WriteLine(mi);
}
}
This will output:
System.String Bar(T)
System.String Bar(Int32)
System.String Bar(Int32)
System.String Bar(Int32)

Try looking at the generic type definition: typeof(Foo<>). The methods will be in the same order.
public class Foo<T> {
public string Bar(T value) { return "Called Bar(T)"; }
public string Bar(int value) { return "Called Bar(int)"; }
public static void CallBar<TR>(Foo<TR> foo) {
var footinfo = typeof(Foo<>).GetMethods();
int i;
for (i = 0; i < footinfo.Count(); ++i) {
if (footinfo[i].Name == "Bar" && footinfo[i].GetParameters()[0].ParameterType.IsGenericParameter == false)
break;
}
Console.WriteLine(foo.GetType().GetMethods()[i].Invoke(foo, new object[] { 0 }));
}
}
// prints Bar(int)...
Foo<int>.CallBar( new Foo<int>() );
The ContainsGenericParameters property is true for both Bar's in Foo<> and false for both Bar's in Foo, so its useless.

I think ContainsGenericParameters is what you're looking for, according to the documentation:
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.isgenericmethod.aspx
http://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.containsgenericparameters.aspx

As Eric Lippert points out, neither of them are generic methods; your class is generic, but you're passing a non-generic instance of the class. Therefore the methods aren't generic the way reflection sees it.
You should be on the right track if you change
foo.GetType()
to
foo.GetGenericTypeDefinition()
For more info, see MSDN's documentation.

Related

Generic class where type can Parse strings

I want to create a generic class where the type of the class can Parse strings.
I want to use this class for any class that has a static function Parse(string),
like System.Int32, System.Double, but also for classes like System.Guid. They all have a static Parse function
So my class needs a where clause that constraints my generic type to types with a Parse function
I'd like to use it like this:
class MyGenericClass<T> : where T : ??? what to do ???
{
private List<T> addedItems = new List<T>()
public void Add(T item)
{
this.AddedItems.Add(item);
}
public void Add(string itemAsTxt)
{
T item = T.Parse(itemAsTxt);
this.Add(item);
}
}
What to write in the where clause?
I was not happy with the answers that would use reflection to do the Parsing.
I prefer a solution that is type safe, so the compiler would complain about the a missing Parse function.
Normally you would constraint to a class that has an interface. But as others said, there is no common interface. Come to think of it I don't need an interface, I need a function that I can call
So my solution would be to insist that creators would provide a delegate to the Parse Function that would parse a string to type T
class MyGenericClass<T>
{
public MyGenericClass(Func<string, T> parseFunc)
{
this.parseFunc = parseFunc;
}
private readonly Func<string, T> parseFunc;
public void Add(string txt)
{
this.Add(parseFunc(txt));
}
}
Usage:
MyGenericClass<Guid> x = new MyGenericClass<Guid>(txt => Guid.Parse(txt));
MyGenericClass<int> y = new MyGenericClass<int> (txt => System.Int32.Parse(txt));
The answer is simpler than I thought
I might be misunderstanding your question, but will this do the trick?
static void Main(string[] args)
{
Guid g = DoParse<Guid>("33531071-c52b-48f5-b0e4-ea3c554b8d23");
}
public static T DoParse<T>(string value)
{
T result = default(T);
MethodInfo methodInfo = typeof(T).GetMethod("Parse");
if (methodInfo != null)
{
ParameterInfo[] parameters = methodInfo.GetParameters();
object classInstance = Activator.CreateInstance(typeof(T), null);
object[] parametersArray = new object[] { value };
result = (T)methodInfo.Invoke(methodInfo, parametersArray);
}
return result;
}
You can´t do that at compile-time at all. All those methods don´t have anything in common except their names. They neither are defined in any common interface nor have they the same parameters.
Imagine you have your own method that accidently has that name, but does something completely different. In addition it´s also an instance-method:
class MyType
{
void Parse() { ... }
}
As you can see that method don´t even has any argument, making it hard to find any common logic between - say - int.Parse(myString)andmyInstanceOfMyType.Parse()`.
But even if you could do that. What would you do with the item? There´s not much as there´s nothing in common between them. In particular it´s impossible to call the methpd, as they are completely different.
You could loop all types with a similar method at runtime however:
var types = allAssemblies.SelectMany(x => x.GetTypes())
.Where(x => x.GetMethod("Parse") != null);
But be aware that this yields to an AmbiguousMatchException if more than one method per type exists with the name, this is when the method is overloaded.

Derived Types and Generics

I have a type that uses generics. Let's call it FlowerDescriptor<T> some flowers are described using numbers, others using strings etc.
so FlowerDescriptor<int>; FlowerDescriptor<string>; etc
I want a mechanism (probably extension methods) for doing 2 things
seeing if something is a FlowerDescriptor and
seeing what the descriptor is.
I.e.
FlowerDescriptor<string>.GetType().IsFlowerDescriptor == true
string.GetType().IsFlowerDescriptor == false.
equally I might derive from FlowerDescriptor<int> i.e. class NumberedFlower: FlowerDescriptor<int>
new NumberedFlower.GetType().IsFlowerDesriptor == true;
as above but returns the type
FlowerDescriptor<string>.GetType().GetFlowerDescriptor() == typeof(string)
FlowerDescriptor<int>.GetType().GetFlowerDescriptor() == typeof(int)
new NumberedFlower.GetType().GetFlowerDescriptor() == typeof(int)
I have played about with variations of IsAssignableFrom and it feels like that ought to work with typeof(FlowerDescriptor<>).IsAssignableFrom(typeof(FlowerDescriptor<string>))
but it doesn't work. If it add the generic type however it does.
I am currently exploring GetInterfaces to know available interfaces. It'd be great to actually understand what I am doing wrong too..
Unless you want to add interfaces into the mix, the only choice you have is to
Detect that the type is actually a FlowerDescriptor<T>
or detect that the type inherits from something that is a FlowerDescriptor<T>
Unfortunately I don't think you can use IsAssignableFrom when it comes to open generics which means we're left with walking the inheritance chain up to the base classes.
Here is an example piece of code that would do the right thing:
public static bool IsFlowerDescriptor(this Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(FlowerDescriptor<>))
return true;
if (type.BaseType != null)
return type.BaseType.IsFlowerDescriptor();
return false;
}
Here's a .NET Fiddle you can experiment with.
I would not expect the string or int class to know if its a descriptor, it makes a lot more sense to get that information from the FlowerDescriptor.
That being said if you want to use reflection you could get the generic type definition from the FlowerDescriptor instance
FlowerDescriptor<int> f = new FlowerDescriptor<int>();
Type t = f.GetType();
Type[] typeArguments = t.GetGenericArguments();
//check if type you care about is in typeArguments
Here is how you would get those two values:
bool isFlowerDescriptor = x is FlowerDescriptor<object>;
Type descriptorType = x.GetType().GetGenericArguments()[0];
You could wrap these in extension methods if you like. And add null-checks etc.
You might consider having a non-generic base class. Then your structure could look like:
public abstract class FlowerDescriptor { }
public class FlowerDescriptor<T> : FlowerDescriptor { }
public class NumberedFlower : FlowerDescriptor<int> { }
Your 2 extensions would be:
public static class Extensions
{
public static bool IsFlowerDescriptor(this object o)
{
return o is FlowerDescriptor;
}
public static Type GetFlowerDescriptor<T>(this FlowerDescriptor<T> o)
{
return typeof (T);
}
}
and you'd use it like:
public static void Main()
{
Console.WriteLine(new NumberedFlower().IsFlowerDescriptor()); //true
Console.WriteLine(new NumberedFlower().GetFlowerDescriptor()); //System.Int32
}
Generics have an adverse effect when it comes to reflecting over and comparing types, because a FlowerDescriptor<int> is a different type from FlowerDescriptor<string>. This is something I have not found a good rhythm for.

Check if object is of non-specific generic type in C#

Say I have the following class:
public class General<T> { }
And I want to find out if an object is of that type.
I know I can use reflection to find out whether the object is of that generic type with Type.GetGenericTypeDefinition, but I want to avoid that.
Is it possible to do something like obj is General<T>, or obj.GetType().IsAssignableFrom(typeof(General<T>))?
I'm quite surprised that I couldn't find a similar question, although I may have used wrong keywords in my searches.
You can do this:
var obj = new General<int>();
var type = obj.GetType();
var isGeneral =
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(General<>)) ||
type.GetBaseTypes().Any(x => x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(General<>));
Where GetBaseTypes is the following extension method:
public static IEnumerable<Type> GetBaseTypes(this Type type)
{
if (type.BaseType == null) return type.GetInterfaces();
return new []{type}.Concat(
Enumerable.Repeat(type.BaseType, 1)
.Concat(type.GetInterfaces())
.Concat(type.GetInterfaces().SelectMany<Type, Type>(GetBaseTypes))
.Concat(type.BaseType.GetBaseTypes()));
}
credits to Slacks answer
There are many answers to similar questions, but they all require reflection to walk up the type hierarchy. I suspect there is no better way. If performance is critical, caching the result maybe an option. Here is an example using a ConcurrentDictionary as a simple cache. Then the cost is reduced to a simple type lookup (via GetType) and a ConcurrentDictionary lookup after the cache has been initialized.
using System.Collections.Concurrent;
private static ConcurrentDictionary<Tuple<Type,Type>, bool> cache = new ConcurrentDictionary<Tuple<Type,Type>, bool>();
public static bool IsSubclassOfRawGeneric(this Type toCheck, Type generic) {
var input = Tuple.Create(toCheck, generic);
bool isSubclass = cache.GetOrAdd(input, key => IsSubclassOfRawGenericInternal(toCheck, generic));
return isSubclass;
}
private static bool IsSubclassOfRawGenericInternal(Type toCheck, Type generic) {
while (toCheck != null && toCheck != typeof(object)) {
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur) {
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}
And you would use it like this:
class I : General<int> { }
object o = new I();
Console.WriteLine(o is General<int>); // true
Console.WriteLine(o.GetType().IsSubclassOfRawGeneric(typeof(General<>))); //true
Generic type definitions that are instantiated with type parameters have no relation at all to other generic type instantiations. They also have no relation to the generic type definition. They are completely incompatible when it comes to assignment and runtime casting. If they weren't it would be possible to break the type system.
For that reason runtime casts will not help. You will indeed have to resort to Type.GetGenericTypeDefinition. You can abstract that into a helper function and keep your code relatively clean that way.
If a generic class or interface has members which could be used by code which held a reference in a more general form like Object but didn't have the actual generic type available, such members should be exposed in a non-generic base class or interface. The Framework has in many cases failed to abide by that principle, but there's no reason one must follow their example. For example, a type like IList<T> could have derived from IListBase which included or inherited members like:
int Count {get;}
void Delete(int index);
void Clear();
void Swap(int index1, int index2);
int Compare(int index1, int index2);
// Return an object with a `StoreToIndex(int)` method
// which would store it to the list it came from.
ListItemHolder GetItemHolder(int index);
ListFeatures Features {get;}
None of those members would rely in any way upon the type of items held within the list, and one could write methods to do things like sort a list (if its Features indicated that it was writable and knew how to compare items) without having to know anything about the element type. If a generic interface inherits from a non-generic interface, code needing the non-generic functions could simply cast to the non-generic interface type and use it directly.
For a more generalized solution, that works with any parent type (base class as well as interfaces):
public static bool IsCompatibleWith(this Type type, Type parentType)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (parentType.IsAssignableFrom(type))
{
return true;
}
return type.GetAssignableTypes()
.Where(t => t.IsGenericType)
.Any(t=> t.GetGenericTypeDefinition() == parentType);
}
/// <summary>
/// Gets all parent types including the currrent type.
/// </summary>
public static IEnumerable<Type> GetAssignableTypes(this Type type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
// First check for interfaces because interface types don't have base classes.
foreach (Type iType in type.GetInterfaces())
{
yield return iType;
}
// Then check for base classes.
do
{
yield return type;
type = type.BaseType;
}
while (type != null);
}
Come up with better method names. Perhaps calling it IsCompatibleWith is misleading. Maybe IsKindOf ? Also, GetAssignableTypes can also be called GetParentTypes but that is also misleading. Naming is hard. Documenting it is better.
Some tests:
IsCompatibleWith(typeof(List<int>), typeof(IList<int>))
true
IsCompatibleWith(typeof(List<>), typeof(IList<>))
true
IsCompatibleWith(typeof(List<int>), typeof(IList<>))
true
IsCompatibleWith(typeof(List<int>), typeof(IList<string>))
false

Can I extract the type of Generic from a return type's type?

So I have, say, this type of method:
public ICollection<String> doSomething() { }
Currently, I'm trying to check if the return type of method is of type ICollection. However, in C#, I have to pass in a generic when I do the check. So I can't do, say, "method is ICollection".
The problem is that I don't want to restrict the type of generic when I'm checking. In Java, I could just use a wildcard, but I can't do that in C#. I've thought of trying to use the Type.GetGenericParamterContraints() and trying to stick the first result of it in ICollection's generic constraint to check, but that also didn't work. Anybody have any ideas?
isCollection(MethodInfo method){
Type[] ret = method.ReturnType.GetGenericParametersContraint();
Type a = ret[0];
return method.ReturnType is ICollection<a>;
}
EDIT: Added what I tried.
If it's allowed to be the non-generic System.Collections.ICollection (which is implemented by ICollection<T> too) then it's simply:
typeof(System.Collections.ICollection).IsAssignableFrom(method.ReturnType)
If you only want to compare to generic ICollection<T> (I see no reason to, but you may have your reasons):
method.ReturnType.IsGenericType
&& typeof(ICollection<>)
.IsAssignableFrom(method.ReturnType.GetGenericTypeDefinition())
Note that that doesn't work if the return type is non-generic. So it won't work if there's a class that implements ICollection<T> but isn't generic itself. Meaning it won't catch class Foo : ICollection<string> but it will catch class Foo<T> : ICollection<T>.
The first way will catch both just fine though.
You should be able to do:
MethodInfo method = ... // up to you
var returnType = method.ReturnType;
var isGenericICollection = returnType == typeof(ICollection<>);
Use Type.GetGenericTypeDefinition(), and compare its result with typeof(ICollection<>).
So, to check if the return type of your method is an ICollection, you could do it like this:
method.ReturnType.GetGenericTypeDefinition() == typeof(ICollection<>)
Btw. method.ReturnType is ICollection<a> will never be true because is checks if the type of the first operand is a subtype of the second operand. ReturnType is of type Type though which is not a subtype of some ICollection.
Try this, Use MethodInfo.ReturnType to determine the return type
Use the below method, call `isCollection<string>(method)`
public static bool isCollection<T>(MethodInfo method)
{
return method.ReturnType.Equals(typeof(ICollection<T>));
}
Try this:
class Program
{
public ICollection<string> Foo() { return new List<string>(); }
public static bool TestType()
{
MethodInfo info = typeof(Program).GetMethod("Foo");
return info.ReturnType.GetGenericTypeDefinition() == typeof(ICollection<>);
}
static void Main(string[] args)
{
Console.WriteLine("{0} is ICollection<> : {1}", "Foo", TestType());
}
}
prints Foo is ICollection<> : True.

How do I enumerate a list of interfaces that are directly defined on an inheriting class?

Updated question given Andrew Hare's correct answer:
Given the following C# classes:
public class Bar : Foo, IDisposable
{
// implementation of Bar and IDisposable
}
public class Foo : IEnumerable<int>
{
// implementation of Foo and all its inherited interfaces
}
I want a method like the following that doesn't fail on the assertions (Note: you cannot change the assertions):
public void SomeMethod()
{
// This doesn't work
Type[] interfaces = typeof(Bar).GetInterfaces();
Debug.Assert(interfaces != null);
Debug.Assert(interfaces.Length == 1);
Debug.Assert(interfaces[0] == typeof(IDisposable));
}
Can someone help by fixing this method so the assertions don't fail?
Calling typeof(Bar).GetInterfaces() doesn't work because it returns the entire interface hierarchy (i.e. interfaces variable contains IEnumerable<int>, IEnumerable, and IDisposable), not just the top level.
Try this:
using System.Linq;
public static class Extensions
{
public static Type[] GetTopLevelInterfaces(this Type t)
{
Type[] allInterfaces = t.GetInterfaces();
var selection = allInterfaces
.Where(x => !allInterfaces.Any(y => y.GetInterfaces().Contains(x)))
.Except(t.BaseType.GetInterfaces());
return selection.ToArray();
}
}
usage:
private void Check(Type t, Type i)
{
var interfaces = t.GetTopLevelInterfaces();
Debug.Assert(interfaces != null, "interfaces is null");
Debug.Assert(interfaces.Length == 1, "length is not 1");
Debug.Assert(interfaces[0] == i, "the expected interface was not found");
System.Console.WriteLine("\n{0}", t.ToString());
foreach (var intf in interfaces)
System.Console.WriteLine(" " + intf.ToString());
}
public void Run()
{
Check(typeof(Foo), typeof(IEnumerable<int>));
Check(typeof(Bar), typeof(IDisposable));
}
As noted elsewhere, this only works if the checked type explicitly implements a single interface. If you have more than one, then you need to change your Assert.
Andrew Hare is correct that you cannot retrieve the specified list of interfaces using reflection. However you can find the "top-level" interfaces by excluding any interfaces that are implied by others. You could implement it like this:
Type[] allInterfaces = typeof(Foo).GetInterfaces();
Type[] interfaces = allInterfaces
.Where(x => !allInterfaces.Any(y => y.GetInterfaces().Contains(x)))
.ToArray();
This passes your assertions.
You just want to get the first level interfaces, right? You could mash up some LINQ and reflection; just exclude anything that the base type is implementing.
var fooType = typeof(Foo);
if(fooType.BaseType == null)
return fooType.GetInterfaces().ToArray();
return fooType
.GetInterfaces()
.Except(fooType.BaseType.GetInterfaces())
.ToArray();
There really isn't any way to do this since you are retrieving all interfaces from the interface hierarchy. This means that when you implement IEnumerable<T> you are also implicitly implementing IEnumerable as well.
In other words, if you look at the IL for the class you have created you will see this:
.class public auto ansi beforefieldinit Foo
extends [mscorlib]System.Object
implements [mscorlib]System.Collections.Generic.IEnumerable`1<int32>,
[mscorlib]System.Collections.IEnumerable
{
// ...
}
Even though you only indicated that your type implements IEnumerable<T>, the compiler has emitted IL that indicates your type implements IEnumerable<T> and IEnumerable.
The reflection API is happily returning what you have actually defined on the type (which is that your type implements both interfaces - which it actually does). The C# compiler allows you to only reference the bottommost type in the interface hierarchy as it will fill in the other interfaces that your type also implements. This is one of the ways that interface inheritance differs from type inheritance.
Note: Updated to also filter inherited interfaces.
You could exclude base interface members, like this:
public Type[] GetDeclaredInterfaces( Type type )
{
if( type == typeof(object) )
return new Type[ 0 ];
Type[] interfaces = type.GetInterfaces();
Type[] baseInterfaces = interfaces.Where( i => i.BaseType != null && i.BaseType.IsInterface );
Type[] declaredInterfaces = interfaces.Except( type.BaseType.GetInterfaces() );
return declaredInterfaces.Except( baseInterfaces );
}
I would write it as:
public void SomeMethod()
{
Type[] interfaces = typeof(Foo).GetInterfaces();
Debug.Assert(interfaces.Contains(typeof(IEnumerable<int>)));
}
But it's hard to answer without knowing what you are trying to test. Regardless, you should not rely on the order when using GetInterfaces and that method will return an empty array if the type doesn't implement any, so the null check is not needed.
Edit: if you really can't change the assertions, then the safe thing to do is:
Type[] allInterfaces = typeof(Foo).GetInterfaces();
var interfaces = allInterfaces.Where(x => x == typeof(IEnumerable<int>)).ToArray();
Debug.Assert(interfaces != null);
Debug.Assert(interfaces.Length == 1);
Debug.Assert(interfaces[0] == typeof(IEnumerable<int>));

Categories

Resources