简体   繁体   English

实例化我的类时需要运行一个受保护的方法

[英]Need to run a protected method when my class is instantiated

I have a base class with some Custom Attributes, this class is the base class for many other clases This is my class: 我有一个带有一些自定义属性的基类,该类是许多其他类的基类。这是我的类:

[CustomAttribute1("First")]
[CustomAttribute2("Other")]
public class Foo
{
    protected void checkAttributes()
    {
        // Need to run this code when the class and derived classes are instantiated 
    }
}

Then if I créate an instance 那如果我创建一个实例

Foo MyClass = new Foo();

or build a dereived class: 或建立一个废弃的类:

[CustomAttribute1("FirstDerived")]
public CustClass : Foo
{
    public CustClass(int MyVar)
    {
        //Something here
    }
    public void OtherMethod()
    {
    }
}

CustClass MyClass = new CustClass(5);

I need the method checkAttributes() allways run. 我需要始终运行方法checkAttributes()

IS that Posible? 那可能吗?

There is another aproach ? 还有另一个方法吗?

NOTE: I need to be shure checkAttributes() runs even if in derived classes the constructor is redefined: 注意:我需要确保checkAttributes()可以运行,即使在派生类中重新定义了构造函数:

Yes, just define a constructor that calls this method: 是的,只需定义一个调用此方法的构造函数即可:

public class Foo
{
    public Foo()
    {
        checkAttributes();
    }

    protected void checkAttributes()
    {
        throw new NotImplementedException();
    }
}

As long as every constructor on Foo winds up calling checkAttributes() (either directly or by chaining to a constructor that does) derived classes will not be able to avoid the check. 只要Foo上的每个构造函数最终都调用checkAttributes() (直接或通过链接到具有该checkAttributes()的构造函数)派生类就无法避免该检查。

(It's unclear why this method would be protected, though. Consider making it private, as it sounds like it is a one-time check run when an object is constructed, and shouldn't need to be run later.) (虽然尚不清楚为什么要保护此方法。但是考虑将其私有化,因为听起来好像在构造对象时是一次检查运行,并且以后不需要运行。)


One of Foo 's constructors must be called (directly or indirectly) by any type that derives from Foo (directly or indirectly). 必须Foo派生的任何类型(直接或间接)调用(直接或间接) Foo的构造函数之一。

From section 10.11.1 of the C# 5 language specification: 根据C#5语言规范的10.11.1节:

All instance constructors (except those for class object ) implicitly include an invocation of another instance constructor immediately before the constructor-body . 所有实例构造函数(类object实例构造函数除外)都隐式包括在Constructor-body之前的另一个实例构造函数的调用。

... ...

If an instance constructor has no constructor initializer, a constructor initializer of the form base() is implicitly provided. 如果实例构造函数没有构造函数初始值设定项,则会隐式提供base()形式的构造函数初始值设定项。

The only way that a derived type can avoid calling a constructor on Foo would be to infinitely recurse into one of its own constructors, which would eventually lead to a StackOverflowException . 派生类型可以避免在Foo上调用构造函数的唯一方法是无限递归到其自己的构造函数中,这最终会导致StackOverflowException

If you want to run it on each object creation of the object you will have to call it in a normal constructor . 如果要在每次创建对象的对象上运行它,则必须在常规构造函数中调用它。

  public Foo()
  {
     //Your normal method call
  }

However if you want it to run only once during your application lifetime you can call in a static constructor Like 但是,如果您希望它在应用程序生存期内仅运行一次,则可以调用静态构造函数,例如

static Foo()
{
    // your static method that needs to be run only once
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当第一次访问静态类是基类上的静态方法时,为什么我的静态对象没有被实例化? - Why are my static objects not being instantiated when first access to the static class is a static method on the base class? 在非静态类中调用静态方法时是否实例化了类? - Is a class instantiated when a static method is called in a non-static class? 实例化方法类时出现错误“对象引用未设置为对象的实例” - Error 'Object reference not set to an instance of an object' when instantiated class of method 定期运行实例化对象的方法 - Periodically run a method of an instantiated object 如何扩展实例化类方法 - How to extend instantiated class method 用方法制作实例化的类数组 - Making an instantiated class array with a method 我需要在Mef实例化类中使用静态TopicClient属性来强制在Web应用程序中使用单个TopicClient吗? - Do I need a static TopicClient property in an Mef instantiated class to enforce the use of a single TopicClient in my web application? 是否需要实例化C#静态类? - Does a C# static class need to be instantiated? 从另一个类调用实例化方法 - Invoking an instantiated method from another class 当我的变量在“清醒”方法中实例化时,为什么会出现NullReferenceException? - Why am I getting a NullReferenceException when my variable is instantiated in the “awake” method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM