I
I
iRumba2015-07-01 08:20:02
.NET
iRumba, 2015-07-01 08:20:02

How to determine if a Delegate instance returns a value?

There is a Dictionary
, I get a certain theGuid, and I must execute what is hidden in the Dictionary under this guid and return a value in case it is required. So how do you know if a value is required? The first thing that comes to mind is try...catch, but somehow it's not pretty.
Like so

if (theDictionary.ContainsKey(theGuid))
{
dynamic FuncOrMethod = theDictionary[theGuid];
try
{
object o = FuncOrMethod(params);
// далее еще один трай будет на выполнение без возврата значений для проверки того, правильно ли переданы параметры
}
}

Another option is to check the type of FuncOrMethod for all types of Action<> and Func<> (and there are 16 of them each, that is, there will be 32 conditions in the switch block, which is also ugly in my opinion)
Is it possible to somehow find out if FuncOrMethod belongs to Action or to Func without checking for all types?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mrrl, 2015-07-01
@Mrl

Try like this:

static bool IsAction(Delegate x){
    return x.Method.ReturnType==typeof(void);
  }

True, I did not check all possible delegates.

T
Tsiren Naimanov, 2015-07-01
@ImmortalCAT

and if these 32 conditions are grouped according to similar semantics? Or at least break it down?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question