你如何实例化一个类没有公共构造函数(和没有静态工厂方法)。具体来说,我试图使用.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类没有公共构造
发布评论