Type.GetType (“namespace.abClassName”)은 null을 반환합니다.
이 코드는 :
Type.GetType("namespace.a.b.ClassName")
을 반환합니다 null
.
그리고 나는 사용에있다 :
using namespace.a.b;
최신 정보:
유형이 존재하고 다른 클래스 라이브러리에 있으며 문자열 이름으로 가져와야합니다.
Type.GetType("namespace.qualified.TypeName")
형식이 mscorlib.dll 또는 현재 실행중인 어셈블리에서 발견 된 경우에만 작동합니다.
위 사항 중 어느 것도 해당되지 않으면 어셈블리 인증 이름 이 필요합니다 .
Type.GetType("namespace.qualified.TypeName, Assembly.Name")
어셈블리 규정 이름없이 dll 이름으로 유형을 가져올 수도 있습니다. 예를 들면 다음과 같습니다.
Type myClassType = Type.GetType("TypeName,DllName");
나는 같은 상황이 있었고 그것이 나를 위해 일했다. "DataModel.QueueObject"유형의 객체가 필요하고 "DataModel"에 대한 참조가 있으므로 다음과 같이 유형을 얻었습니다.
Type type = Type.GetType("DataModel.QueueObject,DataModel");
쉼표 뒤의 두 번째 문자열은 참조 이름 (dll 이름)입니다.
이 방법을 사용해보십시오
public static Type GetType(string typeName)
{
var type = Type.GetType(typeName);
if (type != null) return type;
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
{
type = a.GetType(typeName);
if (type != null)
return type;
}
return null ;
}
Dictionary<string, Type> typeCache;
...
public static bool TryFindType(string typeName, out Type t) {
lock (typeCache) {
if (!typeCache.TryGetValue(typeName, out t)) {
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
t = a.GetType(typeName);
if (t != null)
break;
}
typeCache[typeName] = t; // perhaps null
}
}
return t != null;
}
어셈블리가 ASP.NET 응용 프로그램 빌드의 일부인 경우 BuildManager 클래스를 사용할 수 있습니다.
using System.Web.Compilation
...
BuildManager.GetType(typeName, false);
클래스가 현재 assambly 상태가 아닌 경우 QualifiedName을 제공해야하며이 코드는 클래스의 QualifiedName을 얻는 방법을 보여줍니다.
string qualifiedName = typeof(YourClass).AssemblyQualifiedName;
그리고 QualifiedName으로 타입을 얻을 수 있습니다
Type elementType = Type.GetType(qualifiedName);
중첩 된 Type 인 경우을 변환하는 것을 잊어 버릴 수 있습니다. +
어쨌든, typeof( T).FullName
당신이 말해야 할 것을 말해 줄 것입니다
편집 : BTW 사용 (아시다시피)은 컴파일 타임에 컴파일러에 대한 지시문 일 뿐이므로 API 호출의 성공에 영향을 줄 수 없습니다. (프로젝트 또는 어셈블리 참조가있는 경우 잠재적으로 영향을 줄 수 있으므로 정보가 쓸모가 없으므로 필터링이 필요합니다 ...)
I am opening user controls depending on what user controls the user have access to specified in a database. So I used this method to get the TypeName...
Dim strType As String = GetType(Namespace.ClassName).AssemblyQualifiedName.ToString
Dim obj As UserControl = Activator.CreateInstance(Type.GetType(strType))
So now one can use the value returned in strType to create an instance of that object.
When I have only the class name I use this:
Type obj = AppDomain.CurrentDomain.GetAssemblies().SelectMany(t => t.GetTypes()).Where(t => String.Equals(t.Name, _viewModelName, StringComparison.Ordinal)).First();
If the assembly is referenced and the Class visible :
typeof(namespace.a.b.ClassName)
GetType returns null because the type is not found, with typeof, the compiler may help you to find out the error.
Try using the full type name that includes the assembly info, for example:
string typeName = @"MyCompany.MyApp.MyDomain.MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null";
Type myClassType = Type.GetType(typeName);
I had the same situation when I was using only the the namesspace.classname to get the type of a class in a different assembly and it would not work. Only worked when I included the assembly info in my type string as shown above.
Make sure that the comma is directly after the fully qualified name
typeof(namespace.a.b.ClassName, AssemblyName)
As this wont work
typeof(namespace.a.b.ClassName ,AssemblyName)
I was stumped for a few days on this one
As Type.GetType(String) need the Type.AssemblyQualifiedName you should use Assembly.CreateQualifiedName(String, String).
string typeName = "MyNamespace.MyClass"; // Type.FullName
string assemblyName = "MyAssemblyName"; // MyAssembly.FullName or MyAssembly.GetName().Name
string assemblyQualifiedName = Assembly.CreateQualifiedName(assemblyName , typeName);
Type myClassType = Type.GetType(assemblyQualifiedName);
As assemblyName you don't need to FullName, only the name without the Version, Culture and PublicKeyToken is required.
This solution above seems to be the best to me, but it didn't work for me, so I did it as follows:
AssemblyName assemblyName = AssemblyName.GetAssemblyName(HttpContext.Current.Server.MapPath("~\\Bin\\AnotherAssembly.dll"));
string typeAssemblyQualifiedName = string.Join(", ", "MyNamespace.MyType", assemblyName.FullName);
Type myType = Type.GetType(typeAssemblyQualifiedName);
The precondition is that you know the path of the assembly. In my case I know it because this is an assembly built from another internal project and its included in our project's bin folder.
In case it matters I am using Visual Studio 2013, my target .NET is 4.0. This is an ASP.NET project, so I am getting absolute path via HttpContext
. However, absolute path is not a requirement as it seems from MSDN on AssemblyQualifiedNames
For me, a "+" was the key! This is my class(it is a nested one) :
namespace PortalServices
{
public class PortalManagement : WebService
{
public class Merchant
{}
}
}
and this line of code worked:
Type type = Type.GetType("PortalServices.PortalManagement+Merchant");
I cheated. Since the types I want to create (by name) are all in In a dll I control, I just put a static method in the dll in the assembly that takes a simple name, and calls type.GetType from that context and returns the result.
The original purpose was so that the type could be specified by name in configuration data. I've since change the code so that the user specified a format to process. The format handler classes implement a interface that determines if the type can parse the specified format. I then use reflection to find types that implement the interface, and find one that handles the format. So now the configuration specifies a format name, a not a specific type. The reflection code can look at adjacent dlls and load, them so I have a sort poor man's plug-in architecture.
참고URL : https://stackoverflow.com/questions/1825147/type-gettypenamespace-a-b-classname-returns-null
'Programming' 카테고리의 다른 글
이중 중괄호와 AngularJS-Twig 충돌 (0) | 2020.05.09 |
---|---|
세트를 변환하는 가장 간결한 방법 (0) | 2020.05.09 |
레일즈-컨트롤러 안에서 헬퍼를 사용하는 방법 (0) | 2020.05.09 |
내장 된 Google지도에서 마우스 스크롤 휠 줌 비활성화전체 응용 프로그램을 세로 모드로만 설정하는 방법은 무엇입니까? (0) | 2020.05.09 |
jquery를 사용하여 특정 클래스 이름을 가진 모든 확인란을 선택하십시오. (0) | 2020.05.09 |