You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After benchmarking dynamic vs reflection, I want to use more dynamic, because it seems to be much faster in many situations. Right now I'm using something like this:
This works like a charm. But it'd be great, if something like this would be possible, too:
voidMethod()=>Assert.AreEqual(typeof(bool),GenericMethod<dynamic(true.GetType())>());
Type GenericMethod<T>()whereT: struct =>typeof(T);
The difference here is, that I can use dynamic to call a generic method with an unknown value type - as long as the method consumes a parameter of the generic argument. If there's no parameter, I have no chance to call the GenericMethod, if I have the type of the generic argument T as a Type object only. Currently the only way to call GenericMethod of the 2nd example is using reflections with .MakeGenericMethod(...).Invoke(...) - which runs much slower than dynamic, as my benchmarks showed.
I wouldn't say that I really understand how the DLR works internal, but my idea was to allow dynamic(typeVariable) to be used as generic argument for a type or a method, which would be the most intuitive way for me, if only using a typeVariable would be not a good idea. However, I don't stick to it - I just think it'd be great, if a generic argument value could come from a variable, to be able to avoid using reflection for dynamic generic method calls.
My current workaround in such situations is to add a dummy parameter of the generic argument type for such kinds of method calls. If I want to call such a method dynamic, I use (dynamic)RuntimeHelpers.GetUninitializedObject(typeVariable) to get the default value for the dummy parameter.
That works, but to me it's ugly, because the default value has to be created, but is never being used anywhere in the method (there'll also be an information note IDE0060 raised from Visual Studio). For that reason I created a method which caches the default values.
What do you think - wouldn't dynamic generic arguments enrich C# and complete the DLR ?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
After benchmarking dynamic vs reflection, I want to use more dynamic, because it seems to be much faster in many situations. Right now I'm using something like this:
This works like a charm. But it'd be great, if something like this would be possible, too:
The difference here is, that I can use
dynamic
to call a generic method with an unknown value type - as long as the method consumes a parameter of the generic argument. If there's no parameter, I have no chance to call theGenericMethod
, if I have the type of the generic argumentT
as aType
object only. Currently the only way to callGenericMethod
of the 2nd example is using reflections with.MakeGenericMethod(...).Invoke(...)
- which runs much slower thandynamic
, as my benchmarks showed.I wouldn't say that I really understand how the DLR works internal, but my idea was to allow
dynamic(typeVariable)
to be used as generic argument for a type or a method, which would be the most intuitive way for me, if only using atypeVariable
would be not a good idea. However, I don't stick to it - I just think it'd be great, if a generic argument value could come from a variable, to be able to avoid using reflection for dynamic generic method calls.My current workaround in such situations is to add a dummy parameter of the generic argument type for such kinds of method calls. If I want to call such a method dynamic, I use
(dynamic)RuntimeHelpers.GetUninitializedObject(typeVariable)
to get the default value for the dummy parameter.That works, but to me it's ugly, because the default value has to be created, but is never being used anywhere in the method (there'll also be an information note
IDE0060
raised from Visual Studio). For that reason I created a method which caches the default values.What do you think - wouldn't dynamic generic arguments enrich C# and complete the DLR ?
Beta Was this translation helpful? Give feedback.
All reactions