C# 4.0 无法从DLL获取自定义属性列表
我有一个小要求,完成它似乎有一些困难。请知道,我是c#的新手,这是给我的任务。我恳请大家帮助我,并给予最迅速的答复,因为我已经完成了这项任务的最后期限 我有一个dll,其中定义了一个自定义属性。我希望能够从使用此自定义属性的类中检索所有方法。请注意,我必须从另一个应用程序引用的生成dll中获取方法名称 以下是更清晰的代码 我的属性类:C# 4.0 无法从DLL获取自定义属性列表,c#-4.0,C# 4.0,我有一个小要求,完成它似乎有一些困难。请知道,我是c#的新手,这是给我的任务。我恳请大家帮助我,并给予最迅速的答复,因为我已经完成了这项任务的最后期限 我有一个dll,其中定义了一个自定义属性。我希望能够从使用此自定义属性的类中检索所有方法。请注意,我必须从另一个应用程序引用的生成dll中获取方法名称 以下是更清晰的代码 我的属性类: namespace model { [AttributeUsage(AttributeTargets.Class | AttributeTargets.M
namespace model
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public sealed class Class1: Attribute
{
public Class1()
{}
public Class1(string helptext)
{ }
public string HelpText { get; internal set; }
}
}
使用此属性并在构建为DLL后将被提取的类
private void Form1_Load(object sender, EventArgs e)
{
Assembly mydllAssembly = Assembly.LoadFile(@"D:\Windowsservice\BasicMEthods\BasicMEthods\bin\Debug\BasicMEthods.dll");
Type mydllFormType = mydllAssembly.GetType("BasicMEthods.Transforms",true);
MemberInfo info = mydllFormType;
Attribute[] attrs = (Attribute[])info.GetCustomAttributes(true);
foreach (Attribute att in attrs)
{
MethodInfo[] myArrayMethodInfo1 = mydllFormType.GetMethods(BindingFlags.Public);
for (int i = 0; i < myArrayMethodInfo1.Length; i++)
{
MethodInfo mymethodinfo = (MethodInfo)myArrayMethodInfo1[i];
textBox1.Text = mymethodinfo.ToString();
}
}
}
}
上面说
无法加载文件或程序集“model,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”或其依赖项之一。系统找不到指定的文件
正在从指定位置提取dll,我可以在“快速观察”中看到类转换,我不知道为什么会抛出此错误。。。而且我也不知道如何访问dll中定义的属性……请帮助此方法应该可以实现以下功能:
private List<MethodInfo> FindMethodsWithAttribute(Type T,Type AT)
{
var result = new List<MethodInfo>();
//get all the methods on type T that are public instance methods
var methods=t.GetMethods(BindingFlags.Public);
//loop them
foreach (var method in methods)
{
//get the list of custom attributes, if any, of type AT
var attributes = method.GetCustomAttributes(AT);
if (attributes.Length!=0) result.AddRange(attributes);
}
}
我将留给您自己作为练习,以确定上述内容在现有代码中的位置:)有人请帮助我从DLL中读取自定义属性属性定义了属性类型
model
?听起来像是在另一个程序集中,你还没有加载。谢谢Stephen的回复。。。我没有提交模型组件,后来我意识到了这一点,并提交了模型组件。但是,您能告诉我如何从dll中获得带有自定义属性的方法吗?谢谢您的帮助,查看我的答案,了解应该可以工作的代码。我实际上已经定义了自定义属性。我真的不知道如何搜索用这些属性修饰的方法,而且我对c#是新手,LinQ也不例外……是否有任何参考资料可以供我参考,以便我能找到一些帮助……感谢advanceNo问题,如果答案对你有用,别忘了接受它!您好,我发现一个问题传递模型,因为它是一个dll,是否有其他方法或可能是我不理解它的一个正确的方式…,请您帮助…提前谢谢你好,问题实际上是因为它是一个dll,我不能将模型作为参数发送,因为它是一个dll,由另一个dll引用,从引用这些dll的应用程序中检索。而且GetCustomattribute方法似乎不接受除布尔类型以外的任何值…请建议sumthng…提前感谢为什么您不能直接引用包含模型的dll吗?我无法加载或传递typeof()中的程序集名称,而且GetCustomAttributes()只接受布尔值。
private List<MethodInfo> FindMethodsWithAttribute(Type T,Type AT)
{
var result = new List<MethodInfo>();
//get all the methods on type T that are public instance methods
var methods=t.GetMethods(BindingFlags.Public);
//loop them
foreach (var method in methods)
{
//get the list of custom attributes, if any, of type AT
var attributes = method.GetCustomAttributes(AT);
if (attributes.Length!=0) result.AddRange(attributes);
}
}
var methods = FindMethodsWithAttribute(mydllFormType ,typeof(model));