“set”访问器中的“value”的数据类型是什么?

3

我想知道在C#的set访问器中,value变量的数据类型是什么?

因为我想在C#的set访问器中实现类型提示。

例如,我有一个设置器方法:

public User
{

    private string username;

    public void setUsername(SingleWord username)
    {
        this.username = username.getValue(); // getValue() method from "SingleWord" class returns "string"
    }

}

现在我该如何在C#的访问器语法中实现这个?
public User
{
    public string Username
    {
        get ;
        set {
            // How do I implement type-hinting here for class "SingleWord"?
            // Is it supposed to be:
            // this.Username = ((SingleWord)value).getValue();    ???
        }
    }

}

这样我就可以这样调用它:
User newuser = new User() {
    Username = new SingleWord("sam023")
};

感谢您的提前帮助!编辑:以下是SingleWord的源代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Guitar32.Exceptions;
using Guitar32.Common;

namespace Guitar32.Validations
{
    public class SingleWord : Validator, IStringDatatype
    {
        public static String expression = "^[\\w\\S]+$";
        public static String message = "Spaces are not allowed";
        private String value;

        public SingleWord(String value, bool throwException = false) {
            this.value = value;
            if (throwException && value != null) {
                if (!this.isValid()) {
                    throw new InvalidSingleWordException();
                }
                //if (this.getValue().Length > 0) {
                //    if (!this.isWithinRange()) {
                //        throw new Guitar32.Exceptions.OutOfRangeLengthException();
                //    }
                //}
            }
        }

        public int getMaxLength() {
            return 99999;
        }

        public int getMinLength() {
            return 1;
        }

        public String getValue() {
            return this.value;
        }

        public bool isWithinRange() {
            return this.getValue().Length >= this.getMinLength() && this.getValue().Length <= this.getMaxLength();
        }

        public override bool isValid() {
            return this.getValue().Length > 0 ? Regex.IsMatch(this.getValue(), expression) : true;
        }
    }

    public class InvalidSingleWordException : Exception {
        public InvalidSingleWordException() : base("Value didn't comply to Single Word format")
        { }
    }
}

我使用了这个类来提供后端验证,通过在setter中添加SingleWord作为所需的数据类型。


1
你可以尝试像这里找到的显式类型转换一样的东西: https://msdn.microsoft.com/zh-cn/library/85w54y0a.aspx - Babu James
value 是一个关键字,它通过属性将变量的值设置为相同类型的分配值。 - Mrinal Kamboj
@BabuJames,感谢您提供的显式类型转换的建议。 - Allen Linatoc
1个回答

5
value的类型是属性的类型,不管怎样。

所以在你的例子中,

public string Username
{
    ...
    set
    {
        value.GetType() // -> string
        ...
    }
}

你需要的简单解决方案就是在你的SingleWord实例上调用.getValue()
User newuser = new User()
{
    Username = new SingleWord("sam023").getValue()
};

或者更好的办法是,但我假设这不会生效,因为您没有向我们展示代码。
User newuser = new User()
{
    Username = "sam023"
};

但如果完全不行,那么您想要的似乎是在SingleWord上有一个隐式操作符。如果您有修改类的能力,可以添加一个像这样的操作符,它会自动执行转换为string,以便您可以使用您列出的语法。

public static implicit operator string(SingleWord d)
{
    return d.getValue();
}

无论如何,我添加了我的SingleWord类的源代码。 实际上,我将该类用作我的后端验证。 - Allen Linatoc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接