本文翻译自:Equivalent of typedef in C#
Is there a typedef equivalent in C#, or someway to get some sort of similar behaviour? 在C#中是否存在与typedef等价的东西,或者以某种方式获得了类似的行为? I've done some googling, but everywhere I look seems to be negative. 我已经进行了一些谷歌搜索,但是到处看起来都是负面的。 Currently I have a situation similar to the following: 目前,我的情况类似于以下情况:
class GenericClass<T>
{
public event EventHandler<EventData> MyEvent;
public class EventData : EventArgs { /* snip */ }
// ... snip
}
Now, it doesn't take a rocket scientist to figure out that this can very quickly lead to a lot of typing (apologies for the horrible pun) when trying to implement a handler for that event. 现在,不需要火箭科学家去弄清楚,当尝试为该事件实现处理程序时,这会很快导致很多类型的输入(对可怕的双关语表示歉意)。 It'd end up being something like this: 最终会变成这样:
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 NotImp