I started using lambda expression and I use it often now but only the simple ones :-). Sometime I really get confused understanding lambda expressions in our existing code base. Tried hard to understand the code below but still not able to decipher completely :-(. I think because of the use of Func delegate I am not able to understand. I know that Func delegate is used when the delegate returns some thing. But in this case no clue.
Code snippet:
public class PrintProvider
{
private readonly IInstructionSheetViews _instructionSheetViews;
public PrintProvider(IInstructionSheetViews instructionSheetViews)
{
_instructionSheetViews = instructionSheetViews;
}
public void AddReport()
{
// Some implementation code goes here
var printViews = _instructionSheetViews.PrintViews;
// Some implementation code goes here
}
}
public class InstructionSheetViews : IInstructionSheetViews
{
private readonly IInstructionSheetFactory _factory;
private IEnumerable<IReport> _instructionSheetView;
private List<Func<IInstructionSheetFactory, IReport>> _instructionSheetViewList;
public InstructionSheetViews(IInstructionSheetFactory factory)
{
_factory = factory;
}
public IEnumerable<IReport> PrintViews
{
get
{
if (_instructionSheetView == null)
{
Init();
_instructionSheetView = _instructionSheetViewList.Select(x => x(_factory));
}
return _instructionSheetView;
}
}
private void Init()
{
_instructionSheetViewList = new List<Func<IInstructionSheetFactory, IReport>>();
_instructionSheetViewList.Add(x => x.BuildCommonData());
_instructionSheetViewList.Add(x => x.BuildSpecificData());
}
}
In the above code snippet, AddReport method calls "_instructionSheetViews.PrintViews" and this method inturn calls "Init()".
Q1. What is exactly getting added to "_instructionSheetViewList" here -
_instructionSheetViewList.Add(x => x.BuildCommonData());.
What I can guess is, it adds a method that returns a "IReport". But "_instructionSheetViewList" contains a list of "Func". So, Ideally isn't it that it should contain a method that takes input as "IInstructionSheetFactory" and return "IReport"?
Q2. How does this statement works. Basically the control flow.
_instructionSheetViewList.Select(x => x(_factory));
Can someone please explain me?
Thanks in advance.
So, Ideally isn't it that it should contain a method that takes input as "IInstructionSheetFactory" and return "IReport"?
It does. _instructionSheetViewList.Add(x => x.BuildCommonData()); is basically equivalent to this:
_instructionSheetViewList.Add(anonymousMethod12345);
/*...*/
public static IReport anonymousMethod12345 (IInstructionSheetFactory x)
{
return x.BuildCommonData();
}
_instructionSheetViewList.Add(x => x.BuildCommonData());
is equivalent to -
private IReport GetReport(IInstructionSheetFactory x)
{
return x.BuildCommonData();
}
And when you do this -
_instructionSheetViewList.Select(x => x(_factory));
it actually calls a method with Func as a input parameter to that method
which inturn calls the method referenced by Func delegate with _factory as input parameter
private IReport DoSomething(Func<IInstructionSheetFactory, IReport> x)
{
return x(_factory);
}
Hope this helps.
Related
I have several methods all with the same parameter types and return values but different names and blocks. I want to pass the name of the method to run to another method that will invoke the passed method.
public int Method1(string)
{
// Do something
return myInt;
}
public int Method2(string)
{
// Do something different
return myInt;
}
public bool RunTheMethod([Method Name passed in here] myMethodName)
{
// Do stuff
int i = myMethodName("My String");
// Do more stuff
return true;
}
public bool Test()
{
return RunTheMethod(Method1);
}
This code does not work but this is what I am trying to do. What I don't understand is how to write the RunTheMethod code since I need to define the parameter.
You can use the Func delegate in .NET 3.5 as the parameter in your RunTheMethod method. The Func delegate allows you to specify a method that takes a number of parameters of a specific type and returns a single argument of a specific type. Here is an example that should work:
public class Class1
{
public int Method1(string input)
{
//... do something
return 0;
}
public int Method2(string input)
{
//... do something different
return 1;
}
public bool RunTheMethod(Func<string, int> myMethodName)
{
//... do stuff
int i = myMethodName("My String");
//... do more stuff
return true;
}
public bool Test()
{
return RunTheMethod(Method1);
}
}
You need to use a delegate. In this case all your methods take a string parameter and return an int - this is most simply represented by the Func<string, int> delegate1. So your code can become correct with as simple a change as this:
public bool RunTheMethod(Func<string, int> myMethodName)
{
// ... do stuff
int i = myMethodName("My String");
// ... do more stuff
return true;
}
Delegates have a lot more power than this, admittedly. For example, with C# you can create a delegate from a lambda expression, so you could invoke your method this way:
RunTheMethod(x => x.Length);
That will create an anonymous function like this:
// The <> in the name make it "unspeakable" - you can't refer to this method directly
// in your own code.
private static int <>_HiddenMethod_<>(string x)
{
return x.Length;
}
and then pass that delegate to the RunTheMethod method.
You can use delegates for event subscriptions, asynchronous execution, callbacks - all kinds of things. It's well worth reading up on them, particularly if you want to use LINQ. I have an article which is mostly about the differences between delegates and events, but you may find it useful anyway.
1 This is just based on the generic Func<T, TResult> delegate type in the framework; you could easily declare your own:
public delegate int MyDelegateType(string value)
and then make the parameter be of type MyDelegateType instead.
From OP's example:
public static int Method1(string mystring)
{
return 1;
}
public static int Method2(string mystring)
{
return 2;
}
You can try Action Delegate! And then call your method using
public bool RunTheMethod(Action myMethodName)
{
myMethodName(); // note: the return value got discarded
return true;
}
RunTheMethod(() => Method1("MyString1"));
Or
public static object InvokeMethod(Delegate method, params object[] args)
{
return method.DynamicInvoke(args);
}
Then simply call method
Console.WriteLine(InvokeMethod(new Func<string,int>(Method1), "MyString1"));
Console.WriteLine(InvokeMethod(new Func<string, int>(Method2), "MyString2"));
In order to provide a clear and complete answer, I'm going to start from the very beginning before showing three possible solutions.
A brief introduction
All .NET languages (such as C#, F#, and Visual Basic) run on top of the Common Language Runtime (CLR), which is a VM that runs code in the Common Intermediate Language (CIL), which is way higher level than machine code. It follows that methods aren't Assembly subroutines, nor are they values, unlike functional languages and JavaScript; rather, they're symbols that CLR recognizes. Not being values, they cannot be passed as a parameter. That's why there's a special tool in .NET. That is, delegates.
What's a delegate?
A delegate represents a handle to a method (the term handle is to be preferred over pointer as the latter would be an implementation detail). Since a method is not a value, there has to be a special class in .NET, namely Delegate, which wraps up any method. What makes it special is that, like very few classes, it needs to be implemented by the CLR itself and couldn't be simply written as a class in a .NET language.
Three different solutions, the same underlying concept
The type–unsafe way
Using the Delegate special class directly.
Example:
static void MyMethod()
{
Console.WriteLine("I was called by the Delegate special class!");
}
static void CallAnyMethod(Delegate yourMethod)
{
yourMethod.DynamicInvoke(new object[] { /*Array of arguments to pass*/ });
}
static void Main()
{
CallAnyMethod(MyMethod);
}
The drawback here is your code being type–unsafe, allowing arguments to be passed dynamically, with no constraints.
The custom way
Besides the Delegate special class, the concept of delegates spreads to custom delegates, which are declarations of methods preceded by the delegate keyword. They are type–checked the same way as “normal” method invocations, making for type-safe code.
Example:
delegate void PrintDelegate(string prompt);
static void PrintSomewhere(PrintDelegate print, string prompt)
{
print(prompt);
}
static void PrintOnConsole(string prompt)
{
Console.WriteLine(prompt);
}
static void PrintOnScreen(string prompt)
{
MessageBox.Show(prompt);
}
static void Main()
{
PrintSomewhere(PrintOnConsole, "Press a key to get a message");
Console.Read();
PrintSomewhere(PrintOnScreen, "Hello world");
}
The standard library's way
Alternatively, you can stick with a delegate that's part of the .NET Standard:
Action wraps up a parameterless void method;
Action<T1> wraps up a void method with one parameter of type T1;
Action<T1, T2> wraps up a void method with two parameters of types T1 and T2, respectively,
and so forth;
Func<TR> wraps up a parameterless function with TR return type;
Func<T1, TR> wraps up a function with TR return type and with one parameter of type T1;
Func<T1, T2, TR> wraps up a function with TR return type and with two parameters of types T1 and T2, respectively;
and so forth.
However, bear in mind that by using predefined delegates like these, parameter names won't be self-describing, nor is the name of the delegate type meaningful as to what instances are supposed to do. Therefore, refrain from using them in contexts where their purpose is not absolutely self-evident.
The latter solution is the one most people posted. I'm also mentioning it in my answer for the sake of completeness.
The solution involves Delegates, which are used to store methods to call. Define a method taking a delegate as an argument,
public static T Runner<T>(Func<T> funcToRun)
{
// Do stuff before running function as normal
return funcToRun();
}
Then pass the delegate on the call site:
var returnValue = Runner(() => GetUser(99));
You should use a Func<string, int> delegate, that represents a function taking a string argument and returning an int value:
public bool RunTheMethod(Func<string, int> myMethod)
{
// Do stuff
myMethod.Invoke("My String");
// Do stuff
return true;
}
Then invoke it this way:
public bool Test()
{
return RunTheMethod(Method1);
}
While the accepted answer is absolutely correct, I would like to provide an additional method.
I ended up here after doing my own searching for a solution to a similar question.
I am building a plugin driven framework, and as part of it I wanted people to be able to add menu items to the applications menu to a generic list without exposing an actual Menu object because the framework may deploy on other platforms that don't have Menu UI objects. Adding general info about the menu is easy enough, but allowing the plugin developer enough liberty to create the callback for when the menu is clicked was proving to be a pain. Until it dawned on me that I was trying to re-invent the wheel and normal menus call and trigger the callback from events!
So the solution, as simple as it sounds once you realize it, eluded me until now.
Just create separate classes for each of your current methods, inherited from a base if you must, and just add an event handler to each.
Here is an example Which can help you better to understand how to pass a function as a parameter.
Suppose you have Parent page and you want to open a child popup window. In the parent page there is a textbox that should be filled basing on child popup textbox.
Here you need to create a delegate.
Parent.cs
// declaration of delegates
public delegate void FillName(String FirstName);
Now create a function which will fill your textbox and function should map delegates
//parameters
public void Getname(String ThisName)
{
txtname.Text=ThisName;
}
Now on button click you need to open a Child popup window.
private void button1_Click(object sender, RoutedEventArgs e)
{
ChildPopUp p = new ChildPopUp (Getname) //pass function name in its constructor
p.Show();
}
IN ChildPopUp constructor you need to create parameter of 'delegate type' of parent //page
ChildPopUp.cs
public Parent.FillName obj;
public PopUp(Parent.FillName objTMP)//parameter as deligate type
{
obj = objTMP;
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
obj(txtFirstName.Text);
// Getname() function will call automatically here
this.DialogResult = true;
}
If you want to pass Method as parameter, use:
using System;
public void Method1()
{
CallingMethod(CalledMethod);
}
public void CallingMethod(Action method)
{
method(); // This will call the method that has been passed as parameter
}
public void CalledMethod()
{
Console.WriteLine("This method is called by passing it as a parameter");
}
If the method passed needs to take one argument and return a value, Func is the best way to go. Here is an example.
public int Method1(string)
{
// Do something
return 6;
}
public int Method2(string)
{
// Do something different
return 5;
}
public bool RunTheMethod(Func<string, int> myMethodName)
{
// Do stuff
int i = myMethodName("My String");
Console.WriteLine(i); // This is just in place of the "Do more stuff"
return true;
}
public bool Test()
{
return RunTheMethod(Method1);
}
Read the docs here
However, if your method that is passed as a parameter does not return anything, you can also use Action. It supports up to 16 paramaters for the passed method. Here is an example.
public int MethodToBeCalled(string name, int age)
{
Console.WriteLine(name + "'s age is" + age);
}
public bool RunTheMethod(Action<string, int> myMethodName)
{
// Do stuff
myMethodName("bob", 32); // Expected output: "bob's age is 32"
return true;
}
public bool Test()
{
return RunTheMethod(MethodToBeCalled);
}
Read the documentation here
Here is an example without a parameter:
http://en.csharp-online.net/CSharp_FAQ:_How_call_a_method_using_a_name_string
with params:
http://www.daniweb.com/forums/thread98148.html#
you basically pass in an array of objects along with name of method. you then use both with the Invoke method.
params Object[] parameters
class PersonDB
{
string[] list = { "John", "Sam", "Dave" };
public void Process(ProcessPersonDelegate f)
{
foreach(string s in list) f(s);
}
}
The second class is Client, which will use the storage class. It has a Main method that creates an instance of PersonDB, and it calls that object’s Process method with a method that is defined in the Client class.
class Client
{
static void Main()
{
PersonDB p = new PersonDB();
p.Process(PrintName);
}
static void PrintName(string name)
{
System.Console.WriteLine(name);
}
}
I don't know who might need this, but in case you're unsure how to send a lambda with a delegate, when the function using the delegate doesn't need to insert any params in there you just need the return value.
SO you can also do this:
public int DoStuff(string stuff)
{
Console.WriteLine(stuff);
}
public static bool MethodWithDelegate(Func<int> delegate)
{
///do stuff
int i = delegate();
return i!=0;
}
public static void Main(String[] args)
{
var answer = MethodWithDelegate(()=> DoStuff("On This random string that the MethodWithDelegate doesn't know about."));
}
I have the following function
public static T Translate<T>(T entity)
{
....
}
Now if T is en IEnumerable<> I want to have a different behaviour so I made a second function
public static IEnumerable<T> Translate<T>(IEnumerable<T> entities)
{
....
}
When I invoke it like this
IEnumerable<string> test = new List<string>().AsEnumerable();
Translate(test);
However when I invoke it like this
Func<IEnumerable<string>> func = () => new List<string>().AsEnumerable();
Translate(func.Invoke())
It goes to the first one.
Why does this happen and what is the best construction to solve this?
UPDATE
I build a new example with the problem
static void Main(string[] args)
{
Func<IEnumerable<string>> stringFunction = () => new List<string>().AsEnumerable();
InvokeFunction(ExtendFunction(stringFunction));
}
private static T Convert<T>(T text) where T : class
{
return null;
}
private static IEnumerable<T> Convert<T>(IEnumerable<T> text)
{
return null;
}
private static Func<T> ExtendFunction<T>(Func<T> func) where T : class
{
return () => Convert(func.Invoke());
}
private static T InvokeFunction<T>(Func<T> func)
{
return func.Invoke();
}
The first function gets invoken now when I expect the second to be invoked.
You need to either add a second overload of ExtendFunction:
private static Func<IEnumerable<T>> ExtendFunction<T> (Func<IEnumerable<T>> func) where T : class
{
return () => Convert(func.Invoke());
}
Or make the first overload invoke Convert method dynamically:
private static Func<T> ExtendFunction<T> (Func<T> func) where T : class
{
return () => Convert((dynamic)func.Invoke());
}
The reason is that your ExtendFunction method chooses Convert method at compile time. You can avoid that be either adding a second overload of ExtendFunction which chooses the Convert method you need, or by moving the choice of Convert method to run time.
In C#, the closest to specialization is to use a more-specific overload; however this works only when the type is know at compile time.
In your case the type is decided at run time because of IEnumerable<T>, but the compiler cannot guarantee that it will be IEnumerable<T>.
If you add this line in your main method you'll get the second function called.
Convert(text: new List<string>().AsEnumerable());
this is because the type is know at compile time
so try your Main in this way and look the differences
static void Main(string[] args)
{
Func<IEnumerable<string>> stringFunction = () => new List<string>().AsEnumerable();
InvokeFunction(ExtendFunction(stringFunction));//first function invoked
Convert(text: new List<string>().AsEnumerable());//second function invoked
}
I had the same problem a few weeks ago. You can solve this by specifying the type explicitely on calling the method but that's not really worth it, because it means everybody using your methods have to know about this fact.
We solved our problem by actually giving the method another name. In your case, that would be a second method named:
public static IEnumerable<T> TranslateAll<T>(IEnumerable<T> entities)
I'm having problems getting my head around Action delegates in C#
I've looked at this question and I understand the examples there, but in the codebase that I'm working on it's used in a way that I don't quite get. This is the method using the action delegate:
public static string RenderTemplate<T>(string templatePath, T data)
{
string result = null;
ExecuteRazorMethod(() =>
{
result = Razor.Run(data, templatePath);
});
return result;
}
The call to ExecuteRazorMethod() will invoke the following method:
private static void ExecuteRazorMethod(Action a)
{
try
{
a();
}
.
.
.//irrelevant code
.
}
I don't understand what happens when a() is executed. What method is invoked? I've tried debugging it, and the value of a passed into the method is : Void <RenderTemplate> b__5
I don't get that. I need to know what actually happens to the two parametes in RenderTemplate but not understanding what/where the a() executes makes it hard. Is it maybe something about Anonymous types that I don't understand?
When a executes in ExecuteRazorMethod, it executes exactly the delegate you pass as a to ExecuteRazorMethod. Try to toggle the breakpoint at this line result = Razor.Run(data, templatePath); in this piece of code:
ExecuteRazorMethod(() =>
{
result = Razor.Run(data, templatePath);
});
You will see, that when a starts to execute, your breakpoint will hit.
That code will be compiled to something more or less similar to this, I hope it becomes clear now:
[CompilerGenerated]
internal class Closure<T>
{
public string Result {get; private set;}
private readonly string _templatePath;
private readonly T _data;
public Closure(string templatePath, T data)
{
_templatePath = templatePath;
_data = data;
}
public void DelegateMethod()
{
Result = Razor.Run(_data, _templatePath);
}
}
public static string RenderTemplate<T>(string templatePath, T data)
{
Closure<T> closure = new Closure<T>(templatePath, data);
ExecuteRazorMethod(closure);
return closure.Result;
}
private static void ExecuteRazorMethod<T>(Closure<T> closure)
{
try
{
closure.DelegateMethod();
}
.
.
.//irrelevant code
.
}
The compiler actually creates a named method from the
{ result = Razor.Run(data, templatePath); }
part. That is the Void <RenderTemplate> b__5 method. Remember anonymous types and methods are compiler magic (or syntactic sugar if you prefer), i.e. a C# feature, not a CLR feature. The compiler has to translate them into something the CLR understands.
When the delegate is created, it captures the data and templatePath variables, so it can access them when it's executed. That part is commonly known as closure.
If I have a class like this:
public class SomeClass
{
public Action<string> SomeAction { get; set; }
public SomeClass()
{
SomeAction = GetSomeAnonymousMethod();
}
private Action<string> GetSomeAnonymousMethod()
{
return (text) =>
{
Console.WriteLine(text);
};
}
}
What happens when I make a new instance of SomeClass? My impression is that the constructor simply calls GetSomeAnonymousMethod(), which returns a new delegate instance (that only contains a reference to the compiler-generated backing method for the anonymous method), and assigns it to the SomeAction property.
Can someone confirm, or is something more sinister happening?
Well, that's nearly all that happens. In this particular case, your lambda expression (it's not an anonymous method, to be picky) doesn't need any state, so the generated method can be static, and a delegate reference can be cached. So the "new delegate instance" part may not be correct.
So it's more like this - at least when compiled with the MS compiler:
public class SomeClass
{
private static Action<string> cachedAction;
public Action<string> SomeAction { get; set; }
public SomeClass()
{
SomeAction = GetSomeAnonymousMethod();
}
private Action<string> GetSomeAnonymousMethod()
{
Action<string> action = cachedAction;
if (action == null)
{
action = AnonymousMethodImplementation;
cachedAction = action;
}
return action;
}
private static void AnonymousMethodImplementation(string text)
{
Console.WriteLine(text);
}
}
You don't need to worry about the details of this - and it's all an implementation detail... just a bit of an optimization. But if you really want to know what's going on, that's closer to reality.
As ever, to see the details of what the compiler's doing, you can always use ildasm (or Reflector in IL mode, for example) and see the generated IL.
Can someone confirm, or is something more sinister happening?
It seems like it's another secret action within the events of new and 2nd Cold War!
Yes, your constructor is doing what you're describing in your question.
I would explain that the anonymous delegate is converted into Action<string> using delegate type inference.
For example, the following code line:
return (text) =>
{
Console.WriteLine(text);
};
...is infered to:
return new Action<string>((text) =>
{
Console.WriteLine(text);
});
How can I select the good method (I have in the example below show 2 differents way that doesn't work). I was using instead of a variable of type Object with a IF and IS to do the job but I am trying to avoid using Object and boxing/unboxing. So I thought that Generic could do the job but I am stuck here.
Here is a small snippet of code that illustrate my question:
class Program
{
static void Main(string[] args)
{
Parser p = new Parser();
ObjectType1 o1 = new ObjectType1();
p.execute(o1);
Console.Read();
}
}
class Parser
{
public T execute<T>(T obj)
{
/*
if (obj is ObjectType1)
this.action((ObjectType1)obj);
else if (obj is ObjectType2)
this.action((ObjectType2)obj);
*/
this.action(obj);
return obj;
}
private void action(ObjectType1 objectType1)
{
Console.WriteLine("1");
}
private void action(ObjectType2 objectType2)
{
Console.WriteLine("2");
}
}
class ObjectType1
{
}
class ObjectType2
{
}
Update
I do not want interface and class. Sorry. I knew that it's not the goal of the question.
Casting with (ObjectType)obj doesn't work but if you do :
if (obj is ObjectType1)
this.action(obj as ObjectType1);
else if (obj is ObjectType2)
this.action(obj as ObjectType1);
it works... why?
And... I cannot overload for all type the execute method because this method is from an Interface. This is why all need to be called from this method.
No, you can't do this. Generics don't work like C++ templates - the generic method is compiled just once. The only information that the compiler can use for overload resolution is the information it knows about within the generic method, regardless of what code uses it.
As an example to show this, here's a bit of code which may not work how you expect it to:
using System;
class Test
{
static void Main()
{
string x = "hello";
string y = string.Copy(x);
Console.WriteLine(x==y); // Overload used
Compare(x, y);
}
static void Compare<T>(T x, T y) where T : class
{
Console.WriteLine(x == y); // Reference comparison
}
}
It's hard to say the best way to proceed without knowing more about what you want to do.
Have you considered interfaces?
interface IAction
{
void action();
}
class ObjectType1 : IAction
{
void action() {
Console.WriteLine("1");
}
}
class ObjectType2 : IAction
{
void action() {
Console.WriteLine("2");
}
}
class Parser
{
public IAction execute(IAction obj)
{
obj.action();
return obj;
}
}
Edited by OP:
This solution would require to change all Business Logic Object to have this interface. This is really not a thing to do (in my situation). And, in other situation, I always prefer to have clean BusinessObject that doesn't have Interface not related with Business stuff. In my question, I want a solution that is more related with Generic/Object/Delegate method to achieve it. Thx you. This answer won't be accepted.
The class Parser has a lot of private method that are called by the execute method depending of the object type. It needs to redirect to the good method.
The compiler will do this work for you. Just use overloads.
class Parser
{
public ObjectType1 action(ObjectType1 objectType1)
{
Console.WriteLine("1");
return objectType1;
}
public ObjectType2 action(ObjectType2 objectType2)
{
Console.WriteLine("2");
return objectType2;
}
}
class ObjectType1 { }
struct ObjectType2 { }
Then, called with:
Parser p = new Parser();
p.action(new ObjectType1());
p.action(new ObjectType2());
There's no boxing/unboxing, and the appropriate method gets called.
I haven't tried it, but can you do this?
public T execute<T>(T obj)
{
this.action((T)obj);
return obj;
}
(according to comments, doesn't work)
or
public T execute<T>(T obj)
{
this.action(obj as T);
return obj;
}
(according to comments, works)
I know you're concerned about boxing/unboxing, so there could be ValueTypes involved here.
public T execute<T>(T obj)
{
this.action(obj);
return obj;
}
Supposing that action is modifying obj, and also supposing that modification is important to the caller (which is why you're returning the value back to the caller). This code has a nasty pass-by-value defect.
Consider this code:
public int execute(int obj)
{
this.action(obj);
return obj;
}
public void action(int obj)
{
obj = obj + 1;
}
Called in this way.
int x = p.execute(1);
x is 1, not 2.
Generics happens in compile time. It is best used when you want the same code to apply to different types. It is not dynamic, so it won't help you switch between methods depending on input types.
Overloading resolving as in David B's reply works, but also happens during compile time.
The code in your update does the same thing. It casts (after careful checking of types) and then uses overloading to resolve the method.
I feel that you want to switch methods based on runtime input.
You could get a more dynamic behaviour if you used Reflection.
public object execute(object obj)
{
MethodInfo m = typeof(Parser).GetMethod(
"action",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { obj.GetType() },
null);
m.Invoke(this, new object[] { obj });
return obj;
}
It is perhaps a little fragile, but it works in the example.
IIRC you can use the "where" clause to allow this
public T execute<T>(T obj) where : /* somthing */
{
}
I always have to Google that one my self so I'll leave it at that.
edit: reading some comments. I would not advise calling type specific code. Rather put that code in a virtual function and call that. The call signature might get long, but that's what auto complete is for.
Koodos to joshua.ewer for finding the man page