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.
Related
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.
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.
I was reading C# in depth and I couldn't understand the para of the chapter
Another method—this time one that already existed in .NET 1.1—that’s
worth exploring is Type.GetType(string), and its related
Assembly.GetType(string) method, both of which provide a dynamic
equivalent to typeof. You might expect to be able to feed each line of
the output of listing 3.11 to the GetType method called on an
appropriate assembly, but unfortunately life isn’t quite that
straightforward. It’s fine for closed constructed types—the type
arguments just go in square brackets. For generic type definitions,
though, you need to remove the square brackets entirely— otherwise
GetType thinks you mean an array type. Listing 3.12 shows all of these
methods in action.
and in the example code author did what he asked not to do I believe :
string listTypeName = "System.Collections.Generic.List`1";
Type defByName = Type.GetType(listTypeName);
Type closedByName = Type.GetType(listTypeName + "[System.String]"); // did here ? , since he passed the listTypeName + [Type args] , so compiler should think it's array?
Type closedByMethod = defByName.MakeGenericType(typeof(string));
or might I got the things wrong , can anyone please elaborate with examples what he meant by line "For generic type definitions, though, you need to remove the square brackets entirely— otherwise GetType thinks you mean an array type"
System.Collections.Generic.List`1[System.String] is the name of the closed generic type.
And the name of the open generic type is not System.Collections.Generic.List`1[] but System.Collections.Generic.List`1.
That's basically all that this paragraph says.
.Net type names passed by string use [<type1>(,<typeN>)] to indicate a generic type/function parameter list. [] on it's own indicates an array type.
Also, any generic type name will have the construct:
`n
After its string name to indicate the number of generic parameters that the type has. So:
MyNamespace.MyType`1
References the open generic type MyNamespace.MyType<> (i.e. with no generic arguments supplied).
Whereas
MyNamespace.MyType`1[System.String]
References the closed generic type MyNamespace.MyType<string>.
Note that there are further rules to do with Type.GetType - that being that you can only omit the assembly name (including public key token if applicable) when either:
The type requested can be found in the calling assembly
The type request is in mscorlib.
So, many core types can be specified by string with just their namespace-qualified name, including the generics - and the same goes for when a type name is specified as a type argument (as with System.String above).
I want to get a type from an unreferenced assembly. I tried the suggestion in this question: How to get a type from an unreferenced assembly? like this:
Assembly assembly = Assembly.LoadFrom(#"c:\Path\To\My\Assembly\myAssembly.dll");
Type myType = assembly.GetType("myAssembly.MyClass");
Reading the assembly works, but the GetType method returns null. The requested type (MyClass) is public, so this can't be the problem as a read in one answer on the question which i linked above.
EDIT:
I have to load many different Types from different Assemblies. The type which i want to load is defined in a XML-file. Now the myType.FullName is stored in the XML file and this works. But so I don't get the name of the assembly, where the type is stored. My question is now:
Is it possible to get the type with the GetType-Method by specifying the myType.AssemblyQualifiedName?
I tried this but it didn't work for me - the saved type is null.
This question already has answers here:
Type.GetType("namespace.a.b.ClassName") returns null
(17 answers)
Closed 7 years ago.
I am trying to use Type.GetType and pass "caLibClient.entity.Web2ImageEntity" full class name. The caLibClient.entity is namespace, located in separated assembly (caLibClient) and added to program reference assemblies list. The Type.GetType always return Null when I call it from program, what is wrong?
You need to add the assembly name as well, since your type isn't in the executing assembly (nor mscorlib.) So the call should be:
var myType = Type.GetType("caLibClient.entity.Web2ImageEntity, FullAssemblyName");
From the Type.GetType() docs:
typeName
Type: System.String
The assembly-qualified name of the type to get. See
AssemblyQualifiedName. If the type is in the currently executing
assembly or in Mscorlib.dll, it is sufficient to supply the type name
qualified by its namespace.
From the docs for AssemblyQualifiedName, this is a sample name:
TopNamespace.SubNameSpace.ContainingClass+NestedClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089
Update: If you're already referencing the assembly in your project, and know at compile-time what the type-name is, you're better off saying
Type myType = typeof(caLibClient.entity.Web2ImageEntity);
since now you don't need to search for the type at run-time; the compiler will do everything for you.
Try Type.GetType("caLibClient.entity.Web2ImageEntity, caLibClient"), according to Assembly qualified name
You need to pass an assembly qualified name, in your case something like this:
var yourType = Type.GetType("caLibClient.entity.Web2ImageEntity,caLibClient");
If you know a type in the assembly that the target type lives in you can avoid hard coding the full assembly qualified name. For example:
Type.GetType(
"MyAssembly.Foo.BarSubclass, " + typeof(MyAssembly.Foo.IBar).Assembly.FullName)