2

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?

2
  • Could you clarify, are you trying to populate the dictionary based on an input function? Commented Jan 26, 2016 at 16:23
  • Maybe you could show what you plan to do in the body of the method? Commented Jan 26, 2016 at 16:25

2 Answers 2

1

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"; });
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.