This question already has answers here:
Generating Delegate Types dynamically in C#
(2 answers)
Closed 8 years ago.
In C# can you do something like
Func<typeof(variableType),int)> myDelegate;
where you can pass the type arguments dynamically to a delegate?
No. typeof() is evaluated at run-time. Your delegate declaration is evaluated at compile time. The typeof() evaluation would have to occur first for this to work.
You can not use Func<typeof(variableType),int)> myDelegate;.and get syntax error.
Use:
Func<object,int> myDelegate;
or:
Func<dynamic,int> myDelegate;
And see this:Generating Delegate Types dynamically in C#
Related
This question already has answers here:
Simplify generic type inferring
(3 answers)
Why doesn't C# infer my generic types?
(9 answers)
C# Generic Type Inference With Multiple Types
(2 answers)
C# generic method type argument not inferred from usage
(3 answers)
Closed 1 year ago.
This is a purely aesthetic thing, but I think it is worth asking nonetheless. In the following code:
interface IGiveResult<TResult>
{
TResult GetResult();
}
static class ResultGetter
{
GetResultFrom<TResultGiver, TResult>(TResultGiver giver) where TResultGiver : IGiveResult<TResult>
{
return giver.GetResult();
}
}
To call GetResultFrom, I need to specify both type parameters like this:
ResultGetter.GetResultFrom<SomeGiverType, SomeResultType>(someGiverInstance);
However, I would like to call is like this:
ResultGetter.GetResultFrom<SomeGiverType>(someGiverInstance);
After all, the compiler has all the information neccessary to infer the second generic type. However, this does not compile. Is it possible to change this code, so that one doesn't have to specify the result type?
This question already has answers here:
How do I use reflection to call a generic method?
(8 answers)
Closed 2 years ago.
Why can't I do this?
services.AddSingleton<Type.GetType("ShoppingCartCache",true)>();
OR is there a better way of passing from a string
The method also has an overload to pass a type as parameter. So you can do:
services.AddSingleton(typeof(ShoppingCartCache));
Same also works with your example (although more prone to runtime errors):
services.AddSingleton(Type.GetType("ShoppingCartCache",true));
The reason it doesn't work is because generic types must be static, thus known at compilation.
This question already has answers here:
Why use ThreadStart?
(2 answers)
Threads and delegates — I don't fully understand their relations
(2 answers)
Closed 6 years ago.
Im confused. The docs say a ThreadStart or ParametrizedThreadStart delegate is expected, but a method reference can be passed. How come it accepts a simple reference to a method, why is there no constructor signature that specifically determines this? Or is a delegate functionally identical to a method reference when it comes to type checking?
Im kind of inexperienced when it comes to .net, and this is very confusing to me.
Thanks in advance.
A delegate is a "method reference" of sorts, and by definition a reference to a method is too. So the two are equivalent:
var thread = new Thread(new ThreadStart(MyMethod));
// or
var thread = new Thread(MyMethod); // Assuming Mymethod conforms to the right spec
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C# 2.0 generics: How to create an Action object with zero parameters
I use code to delegate method like this:
this.Invoke((Action)(() => importProcessExited()));
And I'm getting an error:
Using the generic type 'System.Action' requires 1 type arguments.
How this line of code supposed to be in .NET 2?
You can simply create a new delegate without type arguments.
delegate void Action();
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why can't the C# constructor infer type?
Why is the following true:
var foo = new KeyValuePair(3,4); //doesn't compile!
var boo = new KeyValuePair<int,int>(3,4); //works fine!
I would think both lines would be legal, since the type can be (should be) inferred from the parameters. Explanation?
Simply put, type inference only works on methods, not constructors. The reason for this is simple, constructors do not take type arguments, only types and methods do. To wit, KeyValuePair is an undefined type. Remember, it is possible, for example, to have the following types: Action, Action<T>, Action<T1, T2>, etc.