Dictionary of functions with returning value C# - c#

I'm having trouble creating dictionary of functions. The issue I'm running against is how to return value from called method.
I tried something like this but it's not working since I get error about returning value from anonymous function. I don't know how to properly invoke lambda since the examples I've seen have no arguments, while I have both input parameters and return value.
private static Dictionary<string, Action<String, String>> dictionary= new Dictionary<string, Action<String, String>>()
{
{"key",
(arg1, arg2) => {
Class instance= new Class();
return instance.MyFunction(arg1, arg2);)
}
}
And I want to call it as
dictionary["key"]("abc", "123");
I know making MyFunction static would be simpler but Class is implementing an interface and I'm working on C# 7.3

You need to use Func , not Action, since Action cannot return a value. The last Type argument to Func is the return type.
private static Dictionary<string, Func<String, String, String>> dictionary =
new Dictionary<string, Func<String, String, String>>()
{
{
"key",
(arg1, arg2) =>
{
Class instance = new Class();
return instance.MyFunction(arg1, arg2);
}
}
};
More information here: https://learn.microsoft.com/en-us/dotnet/api/system.func-2?view=netcore-3.1

You should use Func delegate instead of Action, since it'll return a value (unlike Action) of type specified by last type argument. Assuming that MyFunction returns string, you can declare it like
private static Dictionary<string, Func<string, string,string>> dictionary =
new Dictionary<string, Func<string, string, string>>
{
{
"key",
(arg1, arg2) =>
{
var instance = new Class();
return instance.MyFunction(arg1, arg2);
}
}
};
And invoke like
var result = dictionary["key"]("abc", "123");
You also should fix a typo with an extra bracket in your code.

Related

How to register lambda expression into Func<>

there is Register function below:
private Dictionary<string, Func<ResolveContainer, object>> _allocFunctionList = ...;
public void Register<T>(Func<ResolveContainer, object> allocFunction)
{
string allocFunctionKey = typeof(T).ToString();
if (_allocFunctionList.ContainsKey(allocFunctionKey))
throw new AllocFunctionKeyExistsException();
_allocFunctionList[allocFunctionKey] = allocFunction;
}
and use case
Register<IFoo>(resolver => new Foo());
Register<IBar>(resolver => new Bar(resolver.Get<IFoo>()));
The Lambda Expression have many kind return type.
So declared return type is object in
Func<ResolveContainer, object>
private Dictionary<string, Func<ResolveContainer, object>> _allocFunctionList = ...;
Question 1
I need to use reflection with the object in the Register Function
but The Register function knows Foo's interface(IFoo) and Func<..., object> -> return type
I want to know Foo and Bar's Type in The Register Function.
how to know Lamda Expression's REAL return class type without update method definition.
Extract real return type in Lambda Expression with any Lib?
or parse Lambda Expression?

C# - Generic method with linq parameter

I'm thinking of creating something like this:
Class.Method<OtherClass>(x => new Dictionary<string, string> { { nameof(x.Property), "Hello World!" } });
I'd like the Method to be void, but somehow I can't get this done. I understand that this signature is
public static void Method<T>(Func<T, Dictionary<string, string>> func)
but I don't want to use the dictionary az a TResult. All I want is to have a dictionary as an input and fill the keys from the T type. Is this possible somehow?
From what I can gather, you're trying to provide a function that populates keys for an existing dictionary. If that's the case, you could do something like this:
public static void Method<T>(Action<T, IDictionary<string, string>> mergeAction);
This can be called like this:
MyClass.Method<OtherClass>((input, dict) => { dict[nameof(input.Property)] = "hello world"; });
A Func<T, TResult> where TResult should return void must be declared as an Action<T>

Use Action or FUNC in dictionaries

i was wondering if it is possible to pass a template into a function like this example:
Dictionary<string, Action<object>> myDict= new Dictionary<string, Action<object>>();
myDict.Add("someString",(nameOfMethod));
this method for example is a setter so it receives as a parameter (double or string or int etc...) and return void.
i suppose the following is impossible..
(i got this ERROR : Error The best overloaded method match for 'System.Collections.Generic.Dictionary>.Add(string, System.Action)' has some invalid arguments)
any ideas how to do it?
this method for example is a setter so it receives as a parameter (double or string or int etc...) and return void.
That's the problem - a method accepting a double isn't applicable for Action<object>, because an Action<object> should be able to be invoked with any object reference as the argument.
You can use a lambda expression to cast and convert though:
myDict.Add("somestring", o => MethodAcceptingDouble((double) o));
You could even write a helper method to do it, although unfortunately due to limitations of type inference you need to specify the type argument explicitly.
static class Test
{
static void Main()
{
var dictionary = new Dictionary<string, Action<object>>();
AddAction<double>(dictionary, "somestring", SampleMethod);
}
static void AddAction<T>(Dictionary<string, Action<object>> dictionary,
string name,
Action<T> action)
{
dictionary.Add(name, arg => action((T) arg));
}
static void SampleMethod(double input)
{
}
}

Dynamic Function Mapping

I would like to define the following two functions:
void Map<T>(Func<T, string> mapper);
T Call<T>(string value);
Map needs to store the function that turns a string into a result of type T so that when the "Call" function is called with a type T and a string the appropriate function can be looked up and called.
I was thinking that map could store the function in a dictionary of type Dictionary<Type, Func<object, string>> and then Call could do the casting to the appropriate type but I'm unable to get that to work. Does anyone know how to achieve this?
The first type argument of Func is the input, the second the output: Func<in T, out TResult> -- so you need Func<string, T>.
(The MSDN reference here uses Func<string, string> a fair bit which is annoying.)
Also, the dictionary can't use the type argument T as that's different for each element in the dictionary. Rather, use the superclass of Func<T, TResult> which is Delegate.
This should work:
Dictionary<Type, Delegate> dictionary = new Dictionary<Type, Delegate>();
public void Map<T>(Func<string, T> mapper)
{
dictionary[typeof(T)] = mapper;
}
public T Call<T>(string value)
{
var func = dictionary[typeof(T)] as Func<string, T>;
return func.Invoke(value);
}
You can try and do something like this (there should be a better way, but I can't see it right now):
Dictinary<Type, object> _funcDict = ...;
void Map<T>(Func<T, string>mapper)
{
_funcDict[typeof(T)] = mapper;
}
T Call<T>(string value)
{
var func = (Func<T, string>)_funcDict[typeof(T)]
return func(value);
}
What I don't like, is having an object value type in the dictionary, but I'm not sure how you can avoid it.

C# Pattern to cache actions accepting generic parameters

I want to create a list of actions to perform on a drawing object. Here's what the non-generic code would look like:
private Dictionary<String, Action<Drawing, String>> actions = new Dictionary<String, Action<Drawing, String>>();
private void LoadActions()
{
actions.Add("Height", (d, s) => d.Height = Double.Parse(s));
actions.Add("Width", (d, s) => d.Width = Double.Parse(s));
}
private void ProcessDrawing(Drawing drawing, String prop, String value)
{
actions[prop](drawing, value);
}
The problem I have is that the Drawing class is a generic one (Drawing<T>) so I can't define actions like the following because T is not defined:
Dictionary<String, Action<Drawing<T>, String>> actions = new Dictionary<String, Action<Drawing<T>, String>>();
Without caching the code looks like this:
private void ProcessDrawing<T>(Drawing<T> drawing, String prop, String value)
{
var actions = new Dictionary<String, Action<Drawing<T>, String>>();
actions.Add("Height", (d, s) => d.Height = Double.Parse(s));
actions.Add("Width", (d, s) => d.Width = Double.Parse(s));
actions[prop](drawing, value);
}
So how can I cache a bunch of actions accepting a generic type of parameter?
Thanks,
The base class of all Actions is MulticastDelegate. You would have to define your dict as Dictionary<String,MulticastDelegate> and use appropriate castings after retrieving your actions from the dict.
EDIT:
Tests show that lambda expressions can obviously not be directly assigned to variables of type MulticastDelegate. This is because the type of the lambda expression parameters is inferred from the type of the variable (or method parameter) it is assigned to. Therefore assign it first to a variable with the right Action<> type. Then assign this to MulticastDelegate.
In the example, I show both versions (through a method parameter and through a variable):
public static void CallTestDelegate()
{
TestDelegate((d, s) => d.Height = Single.Parse(s));
}
public static void TestDelegate(Action<RectangleF, string> action)
{
Dictionary<String, MulticastDelegate> dict = new Dictionary<string, MulticastDelegate>();
dict.Add("a1", action);
Action<RectangleF, string> action2 = (d, s) => d.Width = Single.Parse(s);
dict.Add("a2", action2);
var a1 = (Action<RectangleF, string>)dict["a1"];
a1(new RectangleF(), "15");
}
One option is to make Drawing<T> derive from a non-generic interface IDrawing that has Height and Width properties, then change your actions to be of type Action<IDrawing,String>.
Another option (less type-safe) is to make your actions of type Action<dynamic,String>.
T is just a placeholder, make an abstract class or interface representing objects that you want

Categories