You need to use reflection to get the method to start with, then "construct" it by supplying type arguments with MakeGenericMethod : MethodInfo method = typeof(Sample). GetMethod("GenericMethod"); MethodInfo generic = method. MakeGenericMethod(myType); generic.
Invoke(this, null).
You need to use reflection to get the method to start with, then "construct" it by supplying type arguments with MakeGenericMethod: MethodInfo method = typeof(Sample). GetMethod("GenericMethod"); MethodInfo generic = method. MakeGenericMethod(myType); generic.
Invoke(this, null).
Just an addition to the original answer. While this will work: MethodInfo method = typeof(Sample). GetMethod("GenericMethod"); MethodInfo generic = method.
MakeGenericMethod(myType); generic. Invoke(this, null); It is also a little dangerous in that you lose compile-time check for GenericMethod. If you later do a refactoring and rename GenericMethod, this code won't notice and will fail at run time.
Also, if there is any post-processing of the assembly (for example obfuscating or removing unused methods/classes) this code might break too. So, if you know the method you are linking to at compile time, and this isn't called millions of times so overhead doesn't matter, I would change this code to be: Action GenMethod = GenericMethod; //change int by any base type //accepted by GenericMethod MethodInfo method = this.GetType(). GetMethod(GenMethod.Method.Name); MethodInfo generic = method.
MakeGenericMethod(myType); generic. Invoke(this, null); While not very pretty, you have a compile time reference to GenericMethod here, and if you refactor, delete or do anything with GenericMethod, this code will keep working, or at least break at compile time (if for example you remove GenericMethod). Other way to do the same would be to create a new wrapper class, and create it through Activator.
I don't know if there is a better way.
1 In cases where reflection is used to call a method, it's usual that the method name is itself discovered by another method. Knowing the method name in advance isn't common. – Bevan Feb 27 at 21:59 2 Well, I agree for common uses of reflection.
But the original question was how to call "GenericMethod()" If that syntax was allowed, we wouldn't need GetMethod() at all. But for the question "how do I write "GenericMethod"? I think the answer should include a way to avoid losing the compile-time link with GenericMethod.
Now if this question is common or not I don't know, but I do know I had this exact problem yesterday, and that's why I landed in this question. – Adrian Gallero Feb 28 at 21:24 1 You could do GenMethod.Method. GetGenericMethodDefinition() instead of this.GetType().
GetMethod(GenMethod.Method. Name). It’s slightly cleaner and probably safer.
– Daniel Cassidy May 10 at 10:10.
This is the same as a question I asked the other week: Reflection and generic types I then covered how to call a generic overloaded method on my blog: aaron-powell.com/reflection-and-generics.
1 Your link resulted in 404 error. :-( – Mark Good Nov 28 '10 at 2:36 the correct link is aaron-powell. Com/reflection-and-generics - in short, he's suggesting to use Linq: MethodInfo method = typeof(Helper).
GetMethods(BindingFlags. Public | BindingFlags. Static).
First(m => m. Name == "GetPropertyValue" && m.GetParameters().Count() == 3); – zcrar70 May 25 at 15:40.
With C# 4.0 reflection isn't necessary as the DLR library can call it using runtime types. Since using the dlr library is kind of a pain dynamically (instead of the C# compiler generating code for you), the open source framework ImpromptuInterface gives you easy cached run time access to the same calls the compiler would generate for you. Var name = InvokeMemberName.
Create; Impromptu. InvokeMemberAction(this, name("GenericMethod", new{myType})); var staticContext = InvokeContext. CreateStatic; Impromptu.
InvokeMemberAction(staticContext(typeof(Sample)), name("StaticMethod", new{myType})).
You need to use reflection to get the method to start with, then "construct" it by supplying type arguments with MakeGenericMethod.
Just an addition to the original answer. While this will work.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.