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:
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:
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:
Why covariance and contravariance do not support value type
(4 answers)
Closed 7 years ago.
Why the compiler doesn't allow to assign ints to objects?
IQueryable<object> objects = null;
IQueryable<int> ints = null;
objects = ints;
Implicit type conversion (a function of covariance) does not apply to all generics. SomeGeneric<ValueType> is not derived from to SomeGeneric<Reference> and thus, it is not valid to cast it even if there is a already an implicit conversion for the type parameters (in this case, boxing).
If you are in C# 4.0, a generic interface can be defined as covariant using ISomeGeneric<out T> and provided that the generic arguments are derived from one another, then you can cast. If the generic arguments are not derived, it is not possible to cast.
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.
This question already has answers here:
obtain generic enumerator from an array
(7 answers)
Closed 8 years ago.
This is a follow up question to Design issues and implementing Enumerable.AsEnumerable<FarPoint.Win.Spread.Row>
I have casted my farpoint row to a 2 d object array like this:
object[,] nthRow = fpSpread2.ActiveSheet.GetArray(e.Row, e.Column, 1, FarPointSpread1.ActiveSheet.ColumnCount);
I try to cast this to string so that i can apply Linq but i get the following error at runtime:
IEnumerator<string> narry = (IEnumerator<string>)nthRow.GetEnumerator();
Unable to cast object of type 'ArrayEnumerator' to type 'System.Collections.Generic.IEnumerator`1[System.String]'.
How can i resolve this problem?
Even for multidimensional arrays you can call Cast<T> on it.
using System.Linq;
...
nthRow.Cast<string>().GetEnumerator() // returns IEnumerator<string>
But each element in the array should actually be type string. If you want to do formatting like ToString(), you can first cast them into object and then call Select<T>.