C#从类名获取CustomAttributes

C#从类名获取CustomAttributes,c#,custom-attributes,getcustomattributes,C#,Custom Attributes,Getcustomattributes,是否可以从类名中以字符串形式获取自定义属性 像这样的东西(不起作用) 看起来你就快到了 使用object[]atts=Type.GetType(“MyNamesapce.MyClass”).GetCustomAttributes(true); 对我来说完美无瑕 也许你没提到名称空间 检查一下看起来你就快到了 使用object[]atts=Type.GetType(“MyNamesapce.MyClass”).GetCustomAttributes(true); 对我来说完美无瑕 也许你没提到

是否可以从类名中以字符串形式获取自定义属性

像这样的东西(不起作用)


看起来你就快到了

使用
object[]atts=Type.GetType(“MyNamesapce.MyClass”).GetCustomAttributes(true);

对我来说完美无瑕

也许你没提到名称空间


检查一下

看起来你就快到了

使用
object[]atts=Type.GetType(“MyNamesapce.MyClass”).GetCustomAttributes(true);

对我来说完美无瑕

也许你没提到名称空间


检查一下,你就快到了。你错过了命名空间

 Type myType = Type.GetType("System.String");
 object[] atts = myType.GetCustomAttributes(true);
就你而言

Type myType = Type.GetType("YourNameSpace.MyClass");//this should work
有关更多信息,请参阅

var asnames = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
var asmname = asnames.FirstOrDefault(x => x.Name == assemName);
Assembly.Load(asmname);

使用上述代码预加载程序集(如果它存在于引用的程序集中)

您就快到了。你错过了命名空间

 Type myType = Type.GetType("System.String");
 object[] atts = myType.GetCustomAttributes(true);
就你而言

Type myType = Type.GetType("YourNameSpace.MyClass");//this should work
有关更多信息,请参阅

var asnames = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
var asmname = asnames.FirstOrDefault(x => x.Name == assemName);
Assembly.Load(asmname);

使用上述代码预加载程序集(如果它存在于引用的程序集中)

谢谢这适用于“System.String”,但不适用于“Mynamespace.myclass”-它位于单独的dll中,但被引用,并且有一个using语句,因此应该可用。也许我必须对GetAssembly做些什么。谢谢,这适用于“System.String”,但不适用于“Mynamespace.myclass”-它位于一个单独的dll中,但被引用,并且有一个using语句,所以应该可用。也许我必须对GetAssembly.Type myType=Type.GetType(“YourNameSpace.MyClass”)做些什么;为我返回null。是否有办法为引用的程序集获取此信息-可能尚未使用,因此可能未加载(如果.NET进行惰性加载?@user2707101恐怕,这是可能的,但为什么需要预加载程序集?此方法有效:String strFile=assemName+“.dll”;Byte[]bytes=System.IO.File.ReadAllBytes(dllPath+strFile);Assembly assem=Assembly.Load(字节);类型typeDefinition=assem.GetType(assemName+“.Entity.”+entityName);但是一定有更简单的方法来获取类型?@user2707101更新了我的answerType myType=Type.GetType(“YourNameSpace.MyClass”);为我返回null。是否有办法为引用的程序集获取此信息-可能尚未使用,因此可能未加载(如果.NET进行惰性加载?@user2707101恐怕,这是可能的,但为什么需要预加载程序集?此方法有效:String strFile=assemName+“.dll”;Byte[]bytes=System.IO.File.ReadAllBytes(dllPath+strFile);Assembly assem=Assembly.Load(字节);类型typeDefinition=assem.GetType(assemName+“.Entity.”+entityName);但一定有更简单的方法来获取类型?@user2707101更新了我的答案