相关疑难解决方法(0)

如何获得另一个EXE的汇编版本(不是文件版本)?

您可以使用以下命令获取文件版本:

FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo("filename.exe");
Run Code Online (Sandbox Code Playgroud)

但是如何获得特定EXE文件的汇编版本?

.net c#

32
推荐指数
1
解决办法
3万
查看次数

如何检查方法是否具有属性

我有一个示例课程

public class MyClass{

    ActionResult Method1(){
        ....
    } 

    [Authorize]
    ActionResult Method2(){
       ....
    }

    [Authorize]    
    ActionResult Method3(int value){
       ....
    }

}
Run Code Online (Sandbox Code Playgroud)

现在我想要的是编写一个返回true/false的函数,可以像这样执行

var controller = new MyClass();

Assert.IsFalse(MethodHasAuthorizeAttribute(controller.Method1));
Assert.IsTrue(MethodHasAuthorizeAttribute(controller.Method2));
Assert.IsTrue(MethodHasAuthorizeAttribute(controller.Method3));
Run Code Online (Sandbox Code Playgroud)

我到了那个地步

public bool MethodHasAuthorizeAttribute(Func<int, ActionResult> function)
{
    return function.Method.GetCustomAttributes(typeof(AuthorizeAttribute), false).Length > 0;
}
Run Code Online (Sandbox Code Playgroud)

适用于Method3.现在我怎么能以一种将字符串和类作为参数的方式来做那个泛型呢?

c# reflection tdd attributes assert

24
推荐指数
2
解决办法
1万
查看次数

GetCustomAttribute()为AssemblyVersionAttribute返回null

我正在向.NET应用程序添加一个About对话框,我正在查询程序集的属性以显示信息.当我尝试AssemblyVersionAttribute使用GetCustomAttribute()它来检索我的程序集时返回null:

// Works fine
AssemblyTitleAttribute title
    = (AssemblyTitleAttribute)Attribute.GetCustomAttribute(
        someAssembly, typeof(AssemblyTitleAttribute));

// Gets null
AssemblyVersionAttribute version
    = (AssemblyVersionAttribute)Attribute.GetCustomAttribute(
        someAssembly, typeof(AssemblyVersionAttribute));
Run Code Online (Sandbox Code Playgroud)

AssemblyInfo.cs觉得很好.我定义了这些属性:

[assembly: AssemblyTitle("Some Application")]
[assembly: AssemblyVersion("1.0.0.0")]
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?我有一个解决方法,但我想知道为什么上面的代码不起作用.

// Work-around
string version = someAssembly.GetName().Version.ToString();
Run Code Online (Sandbox Code Playgroud)

.net null assemblyversionattribute

21
推荐指数
3
解决办法
5459
查看次数

AssemblyInfo和自定义属性

我想添加自定义属性AssemblyInfo,我创建了一个名为的扩展类AssemblyMyCustomAttribute

[AttributeUsage(AttributeTargets.Assembly)]
public class AssemblyMyCustomAttribute : Attribute
{
    private string myAttribute;

    public AssemblyMyCustomAttribute() : this(string.Empty) { }
    public AssemblyMyCustomAttribute(string txt) { myAttribute = txt; }
}
Run Code Online (Sandbox Code Playgroud)

然后我添加了对该类的引用AssemblyInfo.cs并添加了值

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("My Project")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("My Project")]
[assembly: AssemblyMyCustomAttribute("testing")]
[assembly: AssemblyCopyright("Copyright ©  2016")]
[assembly: AssemblyTrademark("")] …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc assemblyinfo

6
推荐指数
1
解决办法
3766
查看次数