This question already has answers here:
C#: Restricting Types in method parameters (not generic parameters)
(6 answers)
Closed 7 years ago.
I have a method that accepts a Type as a parameter:
public void DoThings(Type x){...}
How can I restrict the passed Type to a known interface?
Use generics instead:
public void DoThings<X>() where X: IKnown { ... }
Otherwise, if you're forced to use a Type instance, you'll have to do a runtime check and throw an exception if x isn't what is expected.
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:
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:
Pass An Instantiated System.Type as a Type Parameter for a Generic Class
(6 answers)
Closed 9 years ago.
I have a class definition like this:
public class LuRequest<T>
{
...
}
I want to create a instance of it by assigning the type T dynamically like
Type t = OtherObject.GetType();
LuRequest< t> inst = new LuRequest<t>();
However it does not allow me do so, everything in the brackets need to be in compile time.
Is there any way I can do that?
Thank you
Try to use Activator.CreateInstance() method.
http://msdn.microsoft.com/en-gb/library/system.activator.createinstance.aspx
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Nullable type as a generic parameter possible?
Is creating a C# generic method that accepts (nullable) value type and reference type possible?
In c# I would like to restrict my generic method to accept only nullable types. Is that possible?
public T Method<T>() where T : somesortofnullablerestriction
{
...
}
You can do this.
public Nullable<T> Method<T>()
{
}