【问题标题】:custom attribute for string property字符串属性的自定义属性
【发布时间】:2015-01-19 12:44:13
【问题描述】:

我有一个问题。 我想做这样的事情:

[PutStars]
public string telephone

例如,PutStars 可以是自定义属性。

PutStars 作用于字符串,因此它替换电话值[333-123456789],并在获取值时检索例如[333-12xxxx789]

有可能吗?

非常感谢!

【问题讨论】:

  • 你有什么类型的项目?网页、WPF、...?
  • 如果我理解正确,你不能对属性做类似的事情。你需要一个辅助方法来为你完成这项工作。
  • 我有一个网络项目

标签: c# .net string filter custom-attributes


【解决方案1】:

您最接近该内置的可能是[PasswordPropertyText],但a:旨在屏蔽整个字段,b:它完全取决于您所使用的UI框架使用寻找这个属性;属性中没有什么是自动的。坦率地说,您最好的选择可能是添加第二个用于 UI 绑定的属性:

public string Telephone {get;set;}

public string TelephoneMasked {
    get { /* your code here */ }
}

并绑定到TelephoneMasked

【讨论】:

  • 我没有 ui 绑定。我需要该值用于其他目的..!
【解决方案2】:

好吧,你可以实现一个辅助方法,并在获取值时调用它:

private string _tel;
public string Tel 
{
  set{ _tel = value; }
  get {
    return _tel.PutStars();
  }
}

public static string PutStars(this string str)
{
 return str.Replace("1", "*");
}

或者,当你得到字符串时,你可以这样做:

var starred = myObj.Tel.PutStars();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2020-07-20
    • 2012-04-08
    • 2011-01-24
    • 1970-01-01
    相关资源
    最近更新 更多