giotto
giotto

Reputation: 39

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?

Upvotes: 2

Views: 195

Answers (2)

Bas
Bas

Reputation: 27115

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"; });

Upvotes: 1

wertzui
wertzui

Reputation: 5750

A Func<T, TResult> where TResult should return void must be declared as an Action<T>

Upvotes: 0

Related Questions