[英]C# Assigning default property for class and operator =
問題1:
我有一個簡單的 winforms 應用程序,我想將我的 Person.Name 屬性數據綁定到一個文本框。 名稱的類型為 StringField。 我最初將 Name 屬性定義為 String。 數據綁定對字符串等值類型非常有效。 我希望 StringField.Value 屬性成為 StringField 的默認屬性。 我想在文本框中看到 StringField.Value 的值,而不是文本“FieldApp.StringField”。
問題2:
我希望能夠使用運算符 = 將字符串分配給 StringField。 此分配將導致設置 StringField.Value 成員。
這能實現嗎?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FieldApp
{
public class StringField
{
public string Value { get; set; }
}
public class Person
{
//private String _Name;
//public String Name
//{
// get { return _Name; }
// set { _Name = value; }
//}
//public Person(string name)
//{
// Name = name;
//}
private StringField _Name;
public StringField Name
{
get { return _Name; }
set { _Name = value; }
}
public Person(string name)
{
Name = new StringField();
Name.Value = name;
}
}
public partial class FieldAppForm : Form
{
Person person = new Person("steve");
public FieldAppForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//our form contains a button1 and textBox1
//this compiles
person.Name.Value = "steve";
//this does not. Is there anyway to accomplish this?
person.Name = "steve";
//steve appears in the textbox
textBox1.DataBindings.Add("Text", person, "Name.Value");
//FieldApp.StringField appears in the textbox
textBox1.DataBindings.Add("Text", person, "Name");
}
}
}
您可以創建一個隱式運算符重載。 然后你可以從這樣的字符串創建StringField
:
StringField field = "value of new object";
string value=(string)field;
知道這會創建一個新的StringField
對象。 我不一定建議你這樣做。
[System.Diagnostics.DebuggerDisplay("{Value}")]
public class StringField
{
public string Value { get; set; }
public static implicit operator StringField(string s)
{
return new StringField { Value = s };
}
public static explicit operator string(StringField f)
{
return f.Value;
}
public override string ToString()
{
return Value;
}
}
重新數據綁定,對於某些綁定目標( PropertyGrid
、 DataGridView
等),您可以使用TypeConverter
執行此操作(見下文)。 不幸的是,這似乎不適用於TextBox
,所以我認為您最好的選擇是簡單地添加一個 shim 屬性(如已經建議的那樣):
string NameString
{
get { return Name.Value; }
set { Name.Value = value; } // or new blah...
}
(並綁定到NameString
)
過去,我使用自定義PropertyDescriptor
實現來回避這一點,但僅僅為了這個是不值得的。
無論如何,一個TypeConverter
示例(適用於PropertyGrid
和DataGridView
):
[TypeConverter(typeof(StringFieldConverter))]
public class StringField
{
public StringField() : this("") { }
public StringField(string value) { Value = value; }
public string Value { get; private set; }
}
class StringFieldConverter : TypeConverter
{
public override bool CanConvertFrom(
ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string)
|| base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(
ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value)
{
string s = value as string;
if (s != null) return new StringField(s);
return base.ConvertFrom(context, culture, value);
}
public override bool CanConvertTo(
ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string)
|| base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(
ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value, Type destinationType)
{
if (destinationType == typeof(string) && value != null
&& value is StringField)
{
return ((StringField)value).Value;
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
您可以通過提供轉換運算符來實現賦值。 鑒於您的類的性質,您還應該覆蓋 Object 方法:
public class StringField {
public string Value { get; set; }
public static implicit operator StringField(string value) {
StringField sf = new StringField();
sf.Value = value;
return sf;
}
public override string ToString() {
return Value;
}
public override bool Equals(object obj) {
if (obj == null || !(obj is StringField)) return false;
return 0 == string.Compare(Value, (obj as StringField).Value);
}
public override int GetHashCode() {
return Value.GetHashCode();
}
}
您可以通過將 Name 屬性映射到類內部的 Name.Value 字段來生成 StringField。
所以你可以像這樣定義 Name 屬性:
string Name
{
get { return _name.Value; }
set { _name.Value = value; }
}
這里 _name 是您的 StringField 變量。
在 C# 中不能覆蓋賦值運算符。 但是,您可以擁有一個屬性來為您進行類型轉換並將該屬性公開給類
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.