c#中是否有类似的类型定义,或者以某种方式获得类似的行为?我在谷歌上搜了一下,但到处都是否定的。目前我的情况类似如下:

class GenericClass<T> 
{
    public event EventHandler<EventData> MyEvent;
    public class EventData : EventArgs { /* snip */ }
    // ... snip
}

现在,当尝试为该事件实现一个处理程序时,这可能很快导致大量输入(为这个可怕的双关语道歉),这并不需要一个火箭科学家来理解。结果是这样的:

GenericClass<int> gcInt = new GenericClass<int>;
gcInt.MyEvent += new EventHandler<GenericClass<int>.EventData>(gcInt_MyEvent);
// ...

private void gcInt_MyEvent(object sender, GenericClass<int>.EventData e)
{
    throw new NotImplementedException();
}

只不过,在我的例子中,我已经使用了复杂类型,而不仅仅是int型。如果能简化一下就好了……

编辑:ie。也许是对EventHandler进行类型定义,而不需要重新定义它来获得类似的行为。


当前回答

这是它的代码,享受吧!,我从dotNetReference中得到的 在命名空间第106行中键入“using”语句 http://referencesource.microsoft.com/#mscorlib/microsoft/win32/win32native.cs

using System;
using System.Collections.Generic;
namespace UsingStatement
{
    using Typedeffed = System.Int32;
    using TypeDeffed2 = List<string>;
    class Program
    {
        static void Main(string[] args)
        {
        Typedeffed numericVal = 5;
        Console.WriteLine(numericVal++);

        TypeDeffed2 things = new TypeDeffed2 { "whatever"};
        }
    }
}

其他回答

这是它的代码,享受吧!,我从dotNetReference中得到的 在命名空间第106行中键入“using”语句 http://referencesource.microsoft.com/#mscorlib/microsoft/win32/win32native.cs

using System;
using System.Collections.Generic;
namespace UsingStatement
{
    using Typedeffed = System.Int32;
    using TypeDeffed2 = List<string>;
    class Program
    {
        static void Main(string[] args)
        {
        Typedeffed numericVal = 5;
        Console.WriteLine(numericVal++);

        TypeDeffed2 things = new TypeDeffed2 { "whatever"};
        }
    }
}

不,没有typedef的真正对等物。你可以在一个文件中使用“using”指令,例如:

using CustomerList = System.Collections.Generic.List<Customer>;

但这只会影响源文件。在C和c++中,我的经验是typedef通常在广泛包含的.h文件中使用——所以一个typedef可以在整个项目中使用。这种能力在c#中是不存在的,因为c#中没有#include功能来允许你将一个文件中的using指令包含到另一个文件中。

幸运的是,您给出的示例确实有一个固定隐式方法组转换。您可以更改您的事件订阅行:

gcInt.MyEvent += gcInt_MyEvent;

:)

我会做

using System.Collections.Generic;
global using CustomerList = List<Customer>;

对于非密封类,只需从它们继承:

public class Vector : List<int> { }

但是对于密封类,可以用这样的基类来模拟类型定义行为:

public abstract class Typedef<T, TDerived> where TDerived : Typedef<T, TDerived>, new()
{
    private T _value;

    public static implicit operator T(Typedef<T, TDerived> t)
    {
        return t == null ? default : t._value;
    }

    public static implicit operator Typedef<T, TDerived>(T t)
    {
        return t == null ? default : new TDerived { _value = t };
    }
}

// Usage examples

class CountryCode : Typedef<string, CountryCode> { }
class CurrencyCode : Typedef<string, CurrencyCode> { }
class Quantity : Typedef<int, Quantity> { }

void Main()
{
    var canadaCode = (CountryCode)"CA";
    var canadaCurrency = (CurrencyCode)"CAD";
    CountryCode cc = canadaCurrency;        // Compilation error
    Concole.WriteLine(canadaCode == "CA");  // true
    Concole.WriteLine(canadaCurrency);      // CAD

    var qty = (Quantity)123;
    Concole.WriteLine(qty);                 // 123
}

自从c# 10.0引入以来,我们现在有了全局的using指令。

global using CustomerList = System.Collections.Generic.List<Customer>;

这在全局作用域中(贯穿整个项目和对它的所有引用)引入CustomerList作为List<Customer>的别名。

虽然我希望能够限制它的范围(例如“内部使用”),但这实际上在c#中实现类型定义变量方面做得很好。