实例化.NET类没有公共构造

编程入门 行业动态 更新时间:2024-10-26 14:27:17
本文介绍了实例化.NET类没有公共构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你如何实例化一个类没有公共构造函数(和没有静态工厂方法)。具体来说,我试图使用.NET ElapsedEventArgs 类,我的自定义定时器类,但创造它的新对象是不可能的。如果你不能实例是怎样做 System.Timers.Timer的办呢?是preferred方式创建延伸的同级 EventArgs的?

解决方案   

如果您不能实例化它如何 System.Timers.Timer的办呢?

ElapsedEventArgs 有一个构造函数,但它的内部,所以只能在同一个组件可以调用它宣布code。你可以看到它在sourceof:

内部ElapsedEventArgs(INT低,诠释高){     长FILETIME =(长)((((ULONG)的高点)<< 32)|(((ULONG)低)及为0xffffffff));     this.signalTime = DateTime.FromFileTime(FILETIME); }

  

时的preferred方式创建延伸的同级 EventArgs的?

是的,我会这么说。

How do you instantiate a class without a public constructor (and without a static factory method). Specifically, I'm trying to use the .NET ElapsedEventArgs class with my custom timer class but creating new objects of it aren't possible. If you can't instantiate it how does System.Timers.Timer do it? Is the preferred way creating a similar class extending EventArgs?

解决方案

If you can't instantiate it how does System.Timers.Timer do it?

ElapsedEventArgs has a constructor, but it's internal, so only code declared in the same assembly can call it. You can see it on sourceof:

internal ElapsedEventArgs(int low, int high) { long fileTime = (long)((((ulong)high) << 32) | (((ulong)low) & 0xffffffff)); this.signalTime = DateTime.FromFileTime(fileTime); }

Is the preferred way creating a similar class extending EventArgs?

Yes, I would say so.

更多推荐

实例化.NET类没有公共构造

本文发布于:2023-11-04 10:13:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557649.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!