如何强制执行静态构造函数? [英] How to force execution of static constructor?

查看:79
本文介绍了如何强制执行静态构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个带有静态构造函数的类,其中类注册自己

能够做某事(使用委托)

问题是永远不会调用静态构造函数,因为没有其他

类引用它。与班级沟通的唯一方法是使用

代表。


这是我的代码:

内部班级工厂< br $>

{


静态工厂()


{


Configurator.RequestNew + = new RequestNewDelegate(OnRequestNew);


}


private static void OnRequestNew(string _objectName,ref ConfigObject

_object)


{


//处理请求


}

}


有没有办法强制执行静态构造函数,使用

属性还是什么?


问候,

Marc Selis


Hi,

I have a class with a static constructor in which the class registers itself
as capable of doing something (using a delegate)
The problem is that the static constructor is never called, as no other
class references it. The only way to communicate with the class is using
the delegate.

Here is my code:
internal class Factory

{

static Factory()

{

Configurator.RequestNew+=new RequestNewDelegate(OnRequestNew);

}

private static void OnRequestNew(string _objectName, ref ConfigObject
_object)

{

//Handle request

}
}

Is there any way to force the static constructor to be executed, using
attributes or something?

Regards,
Marc Selis


推荐答案

Marc Selis< Ma ******* @ work.axi>写道:


< snip>
Marc Selis <Ma*******@work.axi> wrote:

<snip>
有没有办法强制执行静态构造函数,使用
属性或什么?
Is there any way to force the static constructor to be executed, using
attributes or something?




您可以使用Type.TypeInitializer获取类型初始值设定项,然后

调用它。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件



You can get the type initializer using Type.TypeInitializer, and then
invoke that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


嗨Marc,

不是一个非常优雅的解决方案,但你可以添加一个转储方法,只是为了强制

构造函数来被称为:


static void DumpMethod(){}

干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


Marc Selis <马******* @ work.axi>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Hi Marc,
Not a very elegant solution but you can add a dump method, just to force
the constructor to be called:

static void DumpMethod(){}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc Selis" <Ma*******@work.axi> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


我有一个带有静态构造函数的类,其中类将
本身注册为能够执行某些操作(使用委托)
问题是静态构造函数永远不会被调用,因为没有其他类引用它。与班级沟通的唯一方法是使用
代表。

这是我的代码:

内部班级工厂

{

静态工厂()

{

Configurator.RequestNew + = new RequestNewDelegate(OnRequestNew);

}

private static void OnRequestNew(string _objectName,ref ConfigObject
_object)

//
//处理请求
}

}


有没有办法强制使用
属性执行静态构造函数?
<问候,
Marc Selis
Hi,

I have a class with a static constructor in which the class registers itself as capable of doing something (using a delegate)
The problem is that the static constructor is never called, as no other
class references it. The only way to communicate with the class is using
the delegate.

Here is my code:
internal class Factory

{

static Factory()

{

Configurator.RequestNew+=new RequestNewDelegate(OnRequestNew);

}

private static void OnRequestNew(string _objectName, ref ConfigObject
_object)

{

//Handle request

}
}

Is there any way to force the static constructor to be executed, using
attributes or something?

Regards,
Marc Selis



嗨Ignacio& Jon,


感谢您的答案,但您的解决方案都需要其他课程

知道我的工厂课程的存在。

我的设置的重点是避免这种情况。


我要做的是以下内容:


我有一个类(让我们称之为Builder)需要创建一个新实例

一个实现某个-wellknow接口的类型,但是那个Builder

只有某种通用 - 但唯一名称(无类型名称)描述要创建的

类型。 Builder甚至不确定所请求的类型是否存在

。这就是为什么我选择开一个活动,并希望能够创建它的一些工厂能够响应它。


我们的计划是将来提供额外的工厂,但问题是

将它们注册到Builder的活动中。我现在唯一的另一种选择是使用反射,并查看工厂的每个DLL,但我会使用另一种方法来支付



我以为可能有一种方法可以指示加载

程序集或类型的机制来自动调用静态构造函数(或者一些

其他代码)使用属性或东西...


问候,

Marc


" Ignacio Machin(.NET / C#MVP) < ignacio.machin AT dot.state.fl.us>在消息新闻中写了

:ui ************** @ TK2MSFTNGP10.phx.gbl ...
Hi Ignacio & Jon,

Thanks for you answers, but both your solutions require some other class
knowing the existence of my Factory class.
The whole point of my set up was to avoid this.

What I''m trying to do is the following:

I have a class (let''s call it Builder) that needs to create a new instance
of a type that implements a certain -wellknow- interface, but that Builder
only has some kind of generic -but unique- name (no typename) describing the
type to create. The Builder isn''t even sure that the requested type even
exists. That is why I choose to fire an event, and hope that some Factory
that is able to create it would respond to it.

Our plan is to provide extra Factories in the future, but the problem is
registering them to the Builder''s event. The only alternative I have now is
to use reflection, and to look in every DLL for Factories, but I would
rather use another approach.

I thought there might be a way to instruct the mechanism that loads the
assemblies or types to automatically call a static constructor (or some
other code) using attributes or something...

Regards,
Marc

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:ui**************@TK2MSFTNGP10.phx.gbl...
嗨Marc,

不是一个非常优雅的解决方案,但你可以添加一个dump方法,只是为了强制调用构造函数:

static void DumpMethod(){}

干杯,

- Ignacio Machin,
ignacio.machin at dot.state.fl.us
佛罗里达州交通局
Marc Selis <马******* @ work.axi>在消息中写道
新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...
Hi Marc,
Not a very elegant solution but you can add a dump method, just to force
the constructor to be called:

static void DumpMethod(){}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Marc Selis" <Ma*******@work.axi> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...


我有一个带有静态构造函数的类,其中类注册
Hi,

I have a class with a static constructor in which the class registers


本身

能够做某事(使用委托)
问题是静态构造函数永远不会被调用,因为没有其他类引用它。与班级沟通的唯一方式是使用代表


这是我的代码:

内部班级工厂

{

静态工厂()

{

Configurator.RequestNew + = new RequestNewDelegate(OnRequestNew);

}

private static void OnRequestNew(string _objectName,ref
ConfigObject _object)

//
//处理请求
}

}


有没有办法强制使用
属性执行静态构造函数?
<问候,
Marc Selis
as capable of doing something (using a delegate)
The problem is that the static constructor is never called, as no other
class references it. The only way to communicate with the class is using the delegate.

Here is my code:
internal class Factory

{

static Factory()

{

Configurator.RequestNew+=new RequestNewDelegate(OnRequestNew);

}

private static void OnRequestNew(string _objectName, ref ConfigObject _object)

{

//Handle request

}
}

Is there any way to force the static constructor to be executed, using
attributes or something?

Regards,
Marc Selis




这篇关于如何强制执行静态构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆