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.
Related
This question already has answers here:
Is it possible to get c# to use method overload of most specific type rather than base type?
(4 answers)
up-casting in C# and call a specific method based on the derived type
(8 answers)
Closed 3 years ago.
I have a third party API that passes a parameter that is a base type. I handle each derived type separately as they have specialized functionality for each that I need to make use of.
if(parameter is DerivedTypeX) HandleParamerter(parameter as DerivedTypeX);
else if (parameter is DerivedTypeY) HandleParameter(parameter as DerivedTypeY);
...
For some reason, I can't let it go that there must be a more elegant way to handle this parameter than a massive else if block.
This question already has answers here:
C# Language: How to get type of bound but open Generic class?
(4 answers)
Closed 6 years ago.
I am wondering if anyone knows a way to get a representation of a partially open generic type in C#, for example IDictionary<string,>.
What I have tried:
typeof(IDictionary<string,>)
I get: Partially opened type is not permitted in 'typeof' expression compile error.
typeof(IDictionary<,>).MakeGenericType(typeof(string))
I get: ArgumentException (The number of generic arguments provided doesn't equal the arity of the generic type definition. Parameter name: instantiation)
Why I want to do this:
I have a number of scenarios where I need to check if a class I have implements an interface, but in several cases I know that some of the generic type parameters have to be specific (i.e. I want something that implements a dictionary with string keys but I don't care about the value type). I realize there are a number of other ways I could do this (for example, by providing an array of necessary generic parameter types to my method). But in looking at the problem I got curious if there is a way to specify partially open / partially closed generic types, hence the question.
Maybe something like that could answer to your problem:
class CustomDictionary<TValue> : Dictionary<string, TValue>
{
}
And this should work:
typeof(CustomDictionary<>).MakeGenericType(typeof(string))
This question already has an answer here:
Why do I need an Interface for Covariance (out Type)?
(1 answer)
Closed 8 years ago.
Why in .Net templating a generic class is an invariant operation towards generic arguments?
Interfaces and delegates are not, but classes are.
For instance, I would like to be able to assign object of type Expression<Func<string>> to Expression<Func<object>>. As T in Func<T> is "out" and Expression is immutable, it would be reasonable to assign it as I have showed, right?
Had classes allowed variant type parameters, you wouldn't be able to use them in any field, since fields are always (at least sometimes) writable and readable.
That would limit the utility enough to make it not worth it.
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#
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can you use optional parameters in C#?
I found in a project where I'm working[language: c#]. A strange signature(at the least for me) like that:
AccessModifier NameOfMethod(sometype param1, bool prmFlagOrSomething = false)
In the msdn library doesn't exist any reference to this kind of method.
That allow avoid pass the parameter prmFlagOrSomething, in this case prmFlagOrSomething have the value false.
That's what happened, but exist documentation?
What's the real name of this kind of method, or parameter?
That's a default parameter. Or as MS calls it, an "Optional Argument":
http://msdn.microsoft.com/en-us/library/dd264739.aspx
It's just an Optional Argument