【发布时间】:2009-12-22 16:36:20
【问题描述】:
我正在用 C# 编写一个类库 (API)。该类是非静态的,并且包含几个公共事件。是否可以从单独的类中的静态方法触发这些事件? 比如……
class nonStaticDLLCLASS
{
public event Event1;
public CallStaticMethod()
{
StaticTestClass.GoStaticMethod();
}
}
class StaticTestClass
{
public static GoStaticMethod()
{
// Here I want to fire Event1 in the nonStaticDLLCLASS
// I know the following line is not correct but can I do something like that?
(nonStaticDLLCLASS)StaticTestClass.ObjectThatCalledMe.Event1();
}
}
我知道您通常必须创建非静态类的实例才能访问它的方法,但在这种情况下,已经创建了一个实例,而不是尝试访问它的类。
【问题讨论】:
标签: c# events static parent-child non-static