Where are the array types? [duplicate] - c#

This question already has answers here:
What's the magic of arrays in C#
(8 answers)
Closed 9 years ago.
It's not a problem to use MakeArrayType() if we want to make a array type of a specific type, for example, the char array:
typeof(char).MakeArrayType()
Of course it's more intuitive to use typeof(char[]) instead.
And the property Assembly of a type tells us what the assembly where the type is.
So the following code should be a reasonable example to find a type in an assembly:
var chars=new[] { '\x20' };
var typeofCharArray=chars.GetType();
var assembly=typeofCharArray.Assembly;
var doesContain=assembly.GetTypes().Contains(typeofCharArray);
But doesContain says it DOESN'T, it's false. This happens regardless the array type is from MakeArrayType() or typeof(), or an instance's GetType.
There's a doubt that it was forwarded to other assemblies that I've read from Assembly.GetTypes. And I tried:
var assemblyContainsTypeOfCharArray=(
from it in AppDomain.CurrentDomain.GetAssemblies()
let types=it.GetTypes()
where types.Contains(typeof(char[]))
select it).FirstOrDefault();
The interesting thing is assemblyContainsTypeOfCharArray is null.
Where are the array types?

Simply: GetTypes() returns the types that are actually declared in that assembly. The array types are... not. They claim to be from there, but that is just returning the element-type's Assembly information. The array type isn't actually declared in there (it isn't actually declared anywhere - it is an invention of the JIT, on-the-fly).
So basically: the array type lies. Shame on it.

Related

How can I cast an object using a known Type object? [duplicate]

This question already has answers here:
Casting a variable using a Type variable
(11 answers)
Closed 4 years ago.
I have a Type variable and I need to cast another object to it. (one which I know what the type is, but currently its an "object" type). I need to do this for reasons that aren't really important to the answer.
// Pseudocode
MyObjectClass myTypedVar = new MyObjectClass();
Type myKnownType = myTypedVar.GetType();
var anotherObject = (myKnownType) anObjectVarThatIsReallyMyObjectClass;
I've read this page Type Casting an Object using a "Type" Object in C# and I understand but I don't think it applies directly. I anticipate a solution using reflection, but I just haven't been able to figure it out myself.
If I understand you correctly, you could use the Convert.ChangeType method:
var anotherObject = Convert.ChangeType(anObjectVarThatIsReallyMyObjectClass, myKnownType);
You won't get any kind of compile-time checking when using a dynamic type like this though. Please refer to the following blog post for more information about this.
Generic type parameters and dynamic types in C#: https://blog.magnusmontin.net/2014/10/31/generic-type-parameters-and-dynamic-types-in-csharp/

What are the assembly types with name <>c? [duplicate]

This question already has answers here:
Assembly.GetTypes() returns strange type names e.g. "<>c"
(3 answers)
Closed 6 years ago.
I'm loading some assembly types of my Entities and some of then appears with this value "<>c" in the name
what are these types? And how do I ignore them? (whitout load it) If i need...
They're C#-compiler-generated types, e.g. for anonymous types, iterator blocks, closures that capture local variables, and async functions. This is nothing to do with Entity Framework in itself.
If you want to ignore all compiler-generated types, you can check whether the type has the [CompilerGenerated] attribute applied to it. If you want to do it just from the name, then see whether the name contains < or > - if it does, it's not a valid C# type name, and will have been autogenerated.

Extract type at runtime [duplicate]

This question already has answers here:
Type.GetType("namespace.a.b.ClassName") returns null
(17 answers)
Closed 7 years ago.
I have a Type name being passed as an input .
"MyApp.Modules.Common.contact".
Using Activator.CreateInstance how do I construct this type in the method that I'm using.
if I do it like this
Type typ = Type.GetType("MyApp.Modules.Common.contact")
the typ is always null. How do I fix this. Please help.
If you provide just the type name, Type.GetType will look in the currently-executing assembly, and mscorlib - but that's all. If you need to access a type in a different assembly, then either you need to get the assembly name in the type as well, e.g. "MyApp.Modules.Common.contact, MyApp.Modules.Common" (if the assembly name is "MyApp.Modules.Common" - or if you have an Assembly reference, you can use Assembly.GetType(string).
If you have no information about what assembly you should look in, but you're confident that the assembly has been loaded, you could potentially use AppDomain.GetAssemblies() to find the assemblies, and then look through each of those in turn, calling Assembly.GetType until you find a match.
I would encourage you to look at the design of where how the type information is being passed around though - ideally make sure that the assembly information is available.

C# What is a type of System.Object[*]? [duplicate]

This question already has an answer here:
What does System.Double[*] mean
(1 answer)
Closed 8 years ago.
I've got a COM DLL with a function that should be returning an array of objects of a type that is dependant on the arguments passed in.
I know what the return type should be, but what is returned is of type System.Object[*]
Despite searching I have no idea how to cast that to anything useful. Simply trying to cast to the type it should be "someType[]" fails, also casting to System.Object[] fails. What does the * mean in this context in C#?
I'm assuming you mean casting to System.Object[] fails compile-time. Try casting this way:
someType[] myObj = (someType[]) (System.Object) theDllObj.theDllCall();
It's a cast-via-Object-trick I've used many times when the compiler is being grumpy.

How to GetType of List<String> in C#? [duplicate]

This question already has answers here:
Closed 12 years ago.
How can I get the type of "List<String>" in C# using Type.GetType()?
I have already tried:
Type.GetType("List<String>");
Type.GetType("System.Collections.Generic.List.String"); // Or [String]
Note that I can't use typeof since the value I am getting the type of is a string.
You can't get it from "List<String>", but you can get it from Type.GetType:
Type type = Type.GetType("System.Collections.Generic.List`1[System.String]");
You're lucky in this case - both List<T> and string are in mscorlib, so we didn't have to specify the assemblies.
The `1 part specifies the arity of the type: that it has one type parameter. The bit in square brackets specifies the type arguments.
Where are you getting just List<String> from? Can you change your requirements? It's going to be easier than parsing the string, working out where the type parameters are, finding types in different namespaces and assemblies, etc. If you're working in a limited context (e.g. you only need to support a known set of types) it may be easiest to hard-code some of the details.
You can use typeof operator.
typeof(List<String>)
See this question: C# How can I get Type from a string representation
System.Type type = typeof(List<String>);
typeof(List<string>)

Categories

Resources