This question already has answers here:
Create a generic list using reflection
(3 answers)
Closed 8 years ago.
I am trying to initialize a Generic List object from Type name having as String like below:
List<(Type.GetType("CustomClass"))> AvroList = new List<(Type.GetType("CustomClass"))>();
What is the correct way to do it?
Question Update:
Also I need like below which is a statement with error
var AvroList = GetList<Type.GetType("CustomClass")>();
GetList has some logic which returns the list.
List<T> GetList<T>()
{
}
You need to use MakeGenericType method and then use Activator.CreateInstance to create an instance of your generic type:
var type = typeof(List<>).MakeGenericType(Type.GetType("CustomClass"));
var list = (IList)Activator.CreateInstance(type);
Since you don't now the type at compile time, casting IList is best thing you can do.You can also use dynamic but it is not safe either.
Related
This question already has an answer here:
Creating Array Type at runtime
(1 answer)
Closed 9 months ago.
I have a variable prop of type PropertyInfo.
I want to create a Type variable which is an array of the prop.PropertyType.
For normal generic types I can do typeof(myType<>).MakeGenericType(prop.PropertyType) but arrays aren't generics.
The best I've been able to come up with so far is:
Array.CreateInstance(prop.PropertyType, 0).GetType()
But this is really inelligant, am I missing something?
You can use Type.MakeArrayType() to do this:
var arrayType = prop.PropertyType.MakeArrayType();
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:
How to cast a generic type at runtime in c#
(7 answers)
Casting List<object> to List<T> at runtime
(1 answer)
Closed 4 years ago.
How to cast a List of X object, based on the name in string?
I have this case:
public void AssignValue(string className, Dataset.Table table){
Type currentType = Type.GetType("Namespace." + className);
var objectCasted = (List<currentType.GetType()>)result;
}
Then, I'm trying to cast a List of a dynamic object, how I can do it using reflection?
You can't cast to a type that is not known at compile time. Since your object is a List, it implements IList which might be good enough to allow you to access to the methods and properties you need:
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:
Non-read only alternative to anonymous types
(7 answers)
Closed 10 years ago.
I can create an anonymous class easily using the below. However I can't write to it. Is there a way I can write to some sort of unnamed class?
var test = new { a = 5, b = "sz" };
test.a++;
This is not possible with anonymous types because the properties of an anonymous class are read-only. As to why, you can read here:
Why are the properties of anonymous types in C# read-only?