与C#中的typedef等效

本文翻译自: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 NotImplementedException();
}

Except, in my case, I was already using a complex type, not just an int. 除了我而言,我已经在使用复杂类型,而不仅仅是int类型。 It'd be nice if it were possible to simplify this a little... 如果可以简化一点,那就太好了...

Edit: ie. 编辑:即。 perhaps typedefing the EventHandler instead of needing to redefine it to get similar behaviour. 也许可以对EventHandler进行类型定义,而不必重新定义它以获得类似的行为。


#1楼

参考:https://stackoom.com/question/g0T/与C-中的typedef等效


#2楼

No, there's no true equivalent of typedef. 不,没有typedef的真正等效项。 You can use 'using' directives within one file, eg 您可以在一个文件中使用“ using”指令,例如

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

but that will only impact that source file. 但这只会影响该源文件。 In C and C++, my experience is that typedef is usually used within .h files which are included widely - so a single typedef can be used over a whole project. 在C和C ++中,我的经验是typedef通常用于广泛包含的.h文件中,因此单个typedef可以用于整个项目。 That ability does not exist in C#, because there's no #include functionality in C# that would allow you to include the using directives from one file in another. C#中不存在该功能,因为C#中没有#include功能,该功能允许您将一个文件中的using指令包含到另一个文件中。

Fortunately, the example you give does have a fix - implicit method group conversion. 幸运的是,您提供的示例确实有一个修复方法-隐式方法组转换。 You can change your event subscription line to just: 您可以将事件订阅行更改为:

gcInt.MyEvent += gcInt_MyEvent;

:) :)


#3楼

I think there is no typedef. 我认为没有typedef。 You could only define a specific delegate type instead of the generic one in the GenericClass, ie 您只能定义一个特定的委托类型,而不是GenericClass中的泛型类型,即

public delegate GenericHandler EventHandler<EventData>

This would make it shorter. 这将使其更短。 But what about the following suggestion: 但是以下建议呢?

Use Visual Studio. 使用Visual Studio。 This way, when you typed 这样,当您键入

gcInt.MyEvent += 

it already provides the complete event handler signature from Intellisense. 它已经提供了Intellisense的完整事件处理程序签名。 Press TAB and it's there. 按TAB即可。 Accept the generated handler name or change it, and then press TAB again to auto-generate the handler stub. 接受或更改生成的处理程序名称,然后再次按TAB键以自动生成处理程序存根。


#4楼

Jon really gave a nice solution, I didn't know you could do that! 乔恩确实提供了一个不错的解决方案,但我不知道您能做到这一点!

At times what I resorted to was inheriting from the class and creating its constructors. 有时我求助于从类继承并创建其构造函数。 Eg 例如

public class FooList : List<Foo> { ... }

Not the best solution (unless your assembly gets used by other people), but it works. 不是最好的解决方案(除非您的程序集被其他人使用),但是它可以工作。


#5楼

C# supports some inherited covariance for event delegates, so a method like this: C#支持事件委托的某些继承的协方差,因此这样的方法:

void LowestCommonHander( object sender, EventArgs e ) { ... } 

Can be used to subscribe to your event, no explicit cast required 可用于订阅您的活动,无需显式强制转换

gcInt.MyEvent += LowestCommonHander;

You can even use lambda syntax and the intellisense will all be done for you: 您甚至可以使用lambda语法,智能感知将全部为您完成:

gcInt.MyEvent += (sender, e) =>
{
    e. //you'll get correct intellisense here
};

#6楼

You can use an open source library and NuGet package called LikeType that I created that will give you the GenericClass<int> behavior that you're looking for. 您可以使用我创建的一个开源库和一个名为LikeType的 NuGet包,该包将为您提供所需的GenericClass<int>行为。

The code would look like: 代码如下所示:

public class SomeInt : LikeType<int>
{
    public SomeInt(int value) : base(value) { }
}

[TestClass]
public class HashSetExample
{
    [TestMethod]
    public void Contains_WhenInstanceAdded_ReturnsTrueWhenTestedWithDifferentInstanceHavingSameValue()
    {
        var myInt = new SomeInt(42);
        var myIntCopy = new SomeInt(42);
        var otherInt = new SomeInt(4111);

        Assert.IsTrue(myInt == myIntCopy);
        Assert.IsFalse(myInt.Equals(otherInt));

        var mySet = new HashSet<SomeInt>();
        mySet.Add(myInt);

        Assert.IsTrue(mySet.Contains(myIntCopy));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值