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

与C#中的typedef等效相关推荐

  1. 浅谈C/C++中的typedef和#define

    typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: ...

  2. 27.怎样在Swift中声明typedef?

    在OC中,我们经常会用typedef关键字来声明Block,例如: /*** 通用的空闭包类型,无参数,无返回值*/ typedef void (^GofVoidBlock)(void); 在Swif ...

  3. C/C++中的typedef 和 #define

    C/C++中的typedef 和 #define typedef C/C++中的关键字typedef允许用户为类型名来起一个新名字,通常会是缩写或者能够清晰表明类型含义的新名字. 例: typedef ...

  4. 在 dart fluter 中使用 typedef

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1rfhOkXm-1626739923044)(https://ducafecat.tech/2021/07/20/tra ...

  5. Objective-C中的typedef枚举是什么?

    我不认为我从根本上理解enum是什么,以及何时使用它. 例如: typedef enum {kCircle,kRectangle,kOblateSpheroid } ShapeType; 这里真的被宣 ...

  6. C++中的 typedef Vec<uchar> Vec3b; 是何意?

    预备知识: vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 用法: 1 ...

  7. java实现getch_Java中是否有C++中的getch()等效项? - java

    Java中是否有等同于C++的getch()的代码?该功能可在按下键盘键后立即将控件向前移动,并存储按下的字符. 我想在控制台应用程序中使用该功能. 参考方案 Java中没有等效的getch()函数. ...

  8. C++中typedef和define的区别

    typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译过程的一部分,但它并不实际分配内存空间,实例像: ...

  9. C及C++中typedef的简单使用指南

    C及C++中typedef的简单使用指南 又是在学数据结构的时候,发现了之前学习的知识遗忘很多,在发现对C/C++中关键字typedef的理解还是没有到位后,我翻阅了学C++用到的课本,又问了度娘,也 ...

最新文章

  1. 肠·道 | 刘洋彧:重建肠道菌群生态网络
  2. 人脸识别技术及其应用领域
  3. Java中的Timer和Timer Task详解
  4. php Function split() is deprecated 的解决办法(转)
  5. 【转】做好性能测试的6个关注点
  6. Android初级开发第七讲--特效和数据传递处理
  7. php在线考试系统模板下载,PHPEMS在线模拟考试系统 v6.1
  8. python读取多个txt文件数据恢复_带有Pandas的Python 2.7:如何恢复两个数据帧...
  9. 数据应用apply练习
  10. 办公、学习不得不收藏的十个网站,有了它效率至少提升十倍
  11. python背景怎么自定义铃声_Python 上课铃声的定时播放(具有较强的自我管理意识.jpg)...
  12. 校园 计算机网络设置路由器,Drcom校园网连接路由器怎么设置
  13. 解决Kangle的Easypanel控制面板用户前台php版本无法切换的解决方法
  14. VENDORNPC.LUA --随身商人
  15. 计算机网络连接叹号,网络连接不上,教您网络连接不上显示感叹号
  16. 802.11ax分析1---IEEE 802.11ax和IEEE 802.11ac性能对比
  17. Gas Station (环形加油站)
  18. Linux 5.16 稳定版将带来诸多功能更新
  19. 桌面应用程序UI框架有哪些
  20. 蚂蚁金服大规模分布式事务实践和开源详解

热门文章

  1. Android开发之Dialog的三种列表显示(解读谷歌官方API)
  2. 算法--旋转链表(Java)
  3. 算法------设计哈希映射
  4. Android 数据库 ANR的例子
  5. malloc calloc realloc的对比
  6. 线程池之ScheduledThreadPool学习
  7. 全国自考微型计算机原理及其应用,2010年10月全国自考微型计算机原理及应用试题...
  8. vh与vw的使用事项
  9. 【Learning Spring 5.0】001 Spring架构及Spring介绍
  10. putty-psftp