本文介绍了如何在静态方法中访问实例成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建类来封装电子邮件、URL、电话号码等对象的验证和逻辑.在第一次尝试中,我发现我在所有类中重复了相同的代码,特别是 static IsValidConstructor .所以我决定创建一个基类来将所有相同的代码放入其中.所以有一个基类,其他类继承它.它是抽象,因为我希望它直接使用.

I'm trying to create classes to encapsulate validation and logic for objects like Email , URL , phone number and so on . in the first try I found that I'm repeating the same code in all classes specially static IsValid and the Constructor . so I decided to create a base class to put all the same codes in it . so there is a base class that other classes inherit it . it's abstract as I don't want it to be used directly .

public abstract class BaseClass
{
            protected string value;
            private bool isValid;

            public bool IsValid{get { return this.isValid;}}

    protected virtual string RegexPattern{get;}

    protected virtual RegexOptions RegexOption{get;}

    private BaseClase(){}

    protected BaseClass(string value)
    {
        this.isValid = Validator.IsValid(value , RegexPattern, RegexOption);
        this.value = this.isValid ? value : string.Empty;
    }

    public static bool Validate(string value)
    {
        return Validator.IsValid(value ,RegexPattern, RegexOption); // rror
    }

}

public class Email
{
    private override string RegexPattern
    {
        get
        {
            return ".*";
        }
    }

    private override RegexOptions RegexOption
    {
        get
        {
            return RegexOptions.SingleLine;
        }
    }

            public string Address{get {return this.value; }}

    public Email(string address) : base(address){}
}

问题出在 BaseClass 的静态 IsValid 方法上.在当前代码中,它抛出错误,因为 RegexPatternRegexOption 是实例成员.我不想为 instance 和 static methods 定义两次相同的值.如果我使用

the problem is with the static IsValid method of BaseClass . in the current code it throws errors as RegexPattern and RegexOption are instance members . I don't want to defined the same value twice for instance and static methods . it works if I use

const string RegexPattern= ".*";
const RegexOptions RegexOption =RegexOptions.SingleLine;

但我需要能够在子类中覆盖这些值,所以这不适用.并且由于我已将 BaseClass 定义为 abstract 我无法在静态方法中实例化它以访问属性.

but I need to be able to override these values in sub classes so this is not applicable . and as I have defined the BaseClass as abstract I cant instantiate it inside static method to have access to properties .

并且因为我想像 Email.IsValid("[email protected]"); 一样使用静态方法,所以我不知道如何通过它传递实例.

and as I want to use static method just like Email.IsValid("[email protected]"); , I don't know how to pass an instance through it .

那么,我怎样才能在这个静态方法中访问 RegexOptionRegexPattern ?

so , how can I have access to RegexOption and RegexPattern in this static method ?

推荐答案

你需要有一个实例不知何故,否则它是一个毫无意义的操作.所以问题是 - 您如何希望识别您感兴趣的实例?你真的需要 Validate 是静态的吗?

You need to have an instance somehow, otherwise it's a meaningless operation. So the question is - how would you want to identify the instance that you're interested in? Do you really need Validate to be static at all?

请注意,如果您希望值在整个类的实例中始终保持不变,而不是具有抽象属性,您可能希望将值作为 BaseClass 的构造函数的一部分,并将它们保留在字段中.

Note that instead of having abstract properties, if you expect the values to always stay the same throughout an instance of the class, you might want to make the values part of the constructor for BaseClass instead, and just keep them in fields.

如果您想要实现的是每个子类都有一个单独的验证器,我会将两个问题分开 - 给每个子类一个 不同类型的静态属性.您将无法以多态方式调用它,但听起来您无论如何都不想这样做.

If what you're trying to achieve is that each subclass has a single separate validator, I would separate the two concerns - give each subclass a static property of a different type. You wouldn't be able to call this polymorphically, but it sounds like you don't really want to anyway.

我们真的无法确定您的类在这里代表什么 - 如果您能给我们提供更具体的上下文,我们可能会更有帮助.

We can't really tell what your classes are meant to represent here - if you can give us more concrete context, we can probably be more helpful.

这篇关于如何在静态方法中访问实例成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:16