首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >"set“访问器中"value”的数据类型是什么?

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

Stack Overflow用户
提问于 2015-06-22 03:50:44
回答 1查看 543关注 0票数 3

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

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

例如,我有一个setter方法:

代码语言:javascript
复制
public User
{

    private string username;

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

}

现在,我如何在C#的访问器语法中实现这一点?

代码语言:javascript
复制
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();    ???
        }
    }

}

这样我就可以这样称呼它:

代码语言:javascript
复制
User newuser = new User() {
    Username = new SingleWord("sam023")
};

提前感谢!

编辑:这是SingleWord的源代码

代码语言:javascript
复制
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")
        { }
    }
}

我使用这个类提供后端验证,方法是将SingleWord添加为setter所需的数据类型。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-22 03:59:02

无论发生什么,value类型都是属性的类型。

所以在你的例子中,

代码语言:javascript
复制
public string Username
{
    ...
    set
    {
        value.GetType() // -> string
        ...
    }
}

您所要寻找的简单解决方案就是在您的.getValue()实例上调用SingleWord

代码语言:javascript
复制
User newuser = new User()
{
    Username = new SingleWord("sam023").getValue()
};

或者更好,但我想这是因为你没给我们看的代码,

代码语言:javascript
复制
User newuser = new User()
{
    Username = "sam023"
};

但如果这是绝对禁止的话,听起来你要找的是隐算子 on SingleWord。如果您有能力修改类,您可以添加一个类似于此的操作符,它将自动执行对string的转换,以便您应该能够使用所列出的语法。

代码语言:javascript
复制
public static implicit operator string(SingleWord d)
{
    return d.getValue();
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30971896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档