从静态类触发非静态类?

发布于 2024-08-15 22:11:22 字数 578 浏览 9 评论 0原文

我正在用 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();

   }
}

我知道您通常必须创建非静态类的实例才能访问它的方法,但在这种情况下,实例已经创建,只是不是由尝试访问它的类创建的。

I am writing a class library(API) in C#. The class is non-static and contains several public events. Is it possible to trigger those events from a static method in a separate class?
For example...

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();

   }
}

I know you typically have to create an instance of the non-static class in order to access it's methods but in this case an instance has already been created, just not by the class that is trying to access it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤痕我心 2024-08-22 22:11:22

不可以,实例成员只能在该类型的有效实例上调用/访问。

为了使其工作,您必须将 nonStaticDLLCLASS 的实例传递给 StaticTestClass.GoStaticMethod 并使用该实例引用来调用/访问非静态成员。

在上面的示例中,如何指定您正在访问的类型的实例?静态方法不了解任何实例,那么它如何知道要使用哪一个实例,或者是否有任何实例加载到内存中?

考虑这个例子:

using System;

class Dog
{
    public String Name { get; set; }
}

class Example
{
    static void Main()
    {
        Dog rex = new Dog { Name="Rex" };
        Dog fluffy = new Dog { Name="Fluffy" };
    }
    static void sayHiToDog()
    {
        // In this static method how can I specify which dog instance
        // I mean to access without a valid instance?  It is impossible since
        // the static method knows nothing about the instances that have been
        // created in the static method above.
    }
    static void sayHiToDog(Dog dog)
    {
        // Now this method would work since I now have an instance of the 
        // Dog type that I can say hi to.
        Console.WriteLine("Hello, " + dog.Name);
    }
} 

No, instance members can only be invoked/accessed on a valid instance of the type.

In order for this to work you must pass an instance of nonStaticDLLCLASS to StaticTestClass.GoStaticMethod and use that instance reference to invoke/access the non-static members.

In your example above how do you specify which instance of the type you are accessing? The static method has no knowdlege of any instance so how does it know which one to use or if there are any loaded in memory at all?

Consider this example:

using System;

class Dog
{
    public String Name { get; set; }
}

class Example
{
    static void Main()
    {
        Dog rex = new Dog { Name="Rex" };
        Dog fluffy = new Dog { Name="Fluffy" };
    }
    static void sayHiToDog()
    {
        // In this static method how can I specify which dog instance
        // I mean to access without a valid instance?  It is impossible since
        // the static method knows nothing about the instances that have been
        // created in the static method above.
    }
    static void sayHiToDog(Dog dog)
    {
        // Now this method would work since I now have an instance of the 
        // Dog type that I can say hi to.
        Console.WriteLine("Hello, " + dog.Name);
    }
} 
丑丑阿 2024-08-22 22:11:22

实例方法只能在实例上调用。在您的示例中,实例正在调用静态方法。您能否为静态方法提供一个参数,允许实例传入对其自身的引用?像这样的事情:

class nonStaticDLLCLASS
{
   public event Event1;

   public CallStaticMethod()
  {
     StaticTestClass.GoStaticMethod(this);
  }
}

class StaticTestClass
{
   public static GoStaticMethod(nonStaticDLLCLASS instance)
   {
     // Here I want to fire Event1 in the nonStaticDLLCLASS
     // I know the following line is not correct but can I do something like that?

     instance.Event1();

   }
}

我认为你需要澄清你的问题,以指定为什么你不能做这样的事情,或者为什么实例不能引发自己的事件。

Instance methods can only be called on instances. In your example, the instance is calling the static method. Can you give the static method a parameter allowing the instance to pass in a reference to itself? Something like this:

class nonStaticDLLCLASS
{
   public event Event1;

   public CallStaticMethod()
  {
     StaticTestClass.GoStaticMethod(this);
  }
}

class StaticTestClass
{
   public static GoStaticMethod(nonStaticDLLCLASS instance)
   {
     // Here I want to fire Event1 in the nonStaticDLLCLASS
     // I know the following line is not correct but can I do something like that?

     instance.Event1();

   }
}

I think you need to clarify your question to specify why you can't do something like this, or why the instance can't raise its own event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文