2

Given the property:

Func<dynamic,object> Format { set; get; }

And a method parameter:

void SomeMethod(Func<T, object> format) { // Set Format here.. }

Within this method, how would I set the Format property?

3
  • 4
    This is a weird way to do this. The result will be a function "Format" that takes a dynamic, but will fail if the dynamic cannot be cast to type T. Commented Aug 29, 2013 at 21:17
  • Thus, you will need to handle the cases where it cannot cast the dynamic to be of type T inside the //Set format here. Commented Aug 29, 2013 at 21:44
  • @LeoLorenzoLuis True that, dynamic should be handled with care. Commented Aug 29, 2013 at 21:49

1 Answer 1

4

create a new function that takes the dynamic object, casts it to the object that you need it to be, and then passes that value to the more specific function that you have:

Format = dyn => format((T)dyn); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.