实例化.NET类没有公共构造 [英] Instantiate .NET class without public constructor
问题描述
你如何实例化一个类没有公共构造函数(和没有静态工厂方法)。具体来说,我试图使用.NET ElapsedEventArgs
类,我的自定义定时器类,但创造它的新对象是不可能的。如果你不能实例是怎样做 System.Timers.Timer的
办呢?是preferred方式创建延伸的同级 EventArgs的
?
如果您不能实例化它如何
System.Timers.Timer的
办呢?
ElapsedEventArgs
有一个构造函数,但它的内部,所以只能在同一个组件可以调用它宣布code。你可以看到它在<一个href="http://referencesource.microsoft.com/#System/services/timers/system/timers/ElapsedEventArgs.cs,ea61cae2d875d032,references">sourceof.net:
内部ElapsedEventArgs(INT低,诠释高){
长FILETIME =(长)((((ULONG)的高点)&LT;&LT; 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.net:
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类没有公共构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!