问题描述
我创建了一个公共静态类utils.cs我想在其他班级使用它不与utils的前缀的方法,什么是做到这一点的语法?
I have created a public static class utils.cs I want to use it in other classes without prefixing method with utils, what's the syntax to do this ?
推荐答案
什么乔恩斯基特是不会告诉你的是,你的可以的在C#中全局静态成员。你的实用类的确可以成为C#中的现实。
What Jon Skeet is not telling you is that you can have global static members in c#. Your utility class can indeed become a reality in c#.
不幸的是,微软已确定建设这些成员如上单纯的正常的发展过程本身,并要求您的原中间语言的伪造他们。 。如此强大的模式是上述常见的语法高亮和友好的图标)
Unfortunately, Microsoft has determined that the very process of constructing such members as above mere "normal" development, and requires that you forge them from raw intermediate language. Such a powerful pattern is above common syntax highlighting and friendly icons).
下面是UTF-8字符(小心戒备吧)的所需序列:
Here is the requisite sequence of utf-8 characters (guard it carefully):
.assembly globalmethods {}
.method static public void MyUtilMethod() il managed
{
ldstr "Behold, a global method!"
call void [mscorlib]System.Console::WriteLine(class System.String)
ret
}
(您可以通过在SDK命令提示符下调用编译ilasm.exe这个例子中,记住使用/ DLL开关)
(You could compile this example by invoking ilasm.exe from the SDK command prompt, remembering to use the /dll switch)
ilasm.exe输出:
ilasm.exe output:
微软(R).NET框架IL汇编。版本2.0.50727.4016
版权所有(c)Microsoft公司。版权所有。
组装'globalmethods.msil来DLL - >'globalmethods.dll'
源文件是ANSI
Microsoft (R) .NET Framework IL Assembler. Version 2.0.50727.4016Copyright (c) Microsoft Corporation. All rights reserved.Assembling 'globalmethods.msil' to DLL --> 'globalmethods.dll'Source file is ANSI
global.msil(7):警告 - 引用了未声明的extern集'mscorlib程序。
试图自动检测
global.msil(7) : warning -- Reference to undeclared extern assembly 'mscorlib'.Attempting autodetect
创建PE文件
发光类:
发光属性和方法:
全局的方法:1;
Emitting fields and methods:Global Methods: 1;
发射事件和属性:
全球
写作PE文件
操作已成功完成
Emitting events and properties:GlobalWriting PE fileOperation completed successfully
一旦你编译新创建的组件(如globalmethods.dll为例),它只是一个添加在Visual Studio中引用的问题。该操作完成时,你最好是坐下来,因为这将是一次写一些实际的代码:
Once you have compiled your newly created assembly (as "globalmethods.dll" for example), it's just a matter of adding a reference in Visual Studio. When that is complete, you better be sitting down, because it will be time to write some real code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace TestGlobalMethod
{
class Program
{
static void Main(string[] args)
{
"MyUtilMethod".Call();
Console.ReadKey();
}
}
/// <summary>
/// Reduces the amount of code for global call
/// </summary>
public static class GlobalExtensionMethods
{
public static void Call(this string GlobalMethodName)
{
Assembly.Load("globalmethods")
.GetLoadedModules()[0].GetMethod(GlobalMethodName)
.Invoke(null,null);
}
}
}
是的,你刚才叫C#中的全局方法。
Yes, you just called a Global method in c#.
*请不要使用此,它例如仅:-)此外,你很可能写在另一种语言全局方法支持他们,如VB。 NET。
*Please don't use this, it's for example only :-) Also, you could probably write your global methods in another language that support them, such as VB.NET.
这篇关于如何QUOT&;进出口QUOT; C#中的静态类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!