Runtime error on casting Array to string Enumerator [duplicate] - c#

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>.

Related

Get `Type` for Array of Type at Runtime [duplicate]

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();

How to get the item type of a List? [duplicate]

This question already has answers here:
How to get the type of T from a member of a generic class or method
(17 answers)
What is '1 in Collection type Name
(1 answer)
Closed 1 year ago.
Suppose I have a "Type mytype", and if I do "mytype.ToString()", I get "System.Collections.Generic.List`1[Example1.Employee]", how do I get the list item type, which is "Example1.Employee"? Do I have to parse this string to get this list item type? Is there a more elegant way, such as "mytype.ItemType"?
Also, why is there a "`1" appendix at the end of "System.Collections.Generic.List`1"?
And, why is there a "`2" at the end of "System.Collections.Generic.Dictionary`2"?
Simple:
Type element = list.GetType().GenericTypeArguments[0]
And the "`1" just indicates the number of generic parameters.

Cast to dynamic list with reflection c# [duplicate]

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:

C#: String to Class [duplicate]

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.

Get Enum values as string array [duplicate]

This question already has answers here:
Convert System.Array to string[]
(5 answers)
Closed 8 years ago.
I want to get all Enum.values as string[].
I tryed using
Array mPriorityVals = Enum.GetValues(typeof(MPriority));
But how do I cast it as string[]?
You just need Enum.GetNames method, Enum.GetValues gives the result as EnumType rather than string.
string[] names = Enum.GetNames(typeof (MPriority));
I suggest you to just use GetNames, don't call GetValues and cast it to string as suggested in comment.

Categories