C#为class和operator =分配默认属性

时间:2009-01-11 19:50:25

标签: c# properties default

问题1:

我有一个简单的winforms应用程序,我希望DataBind我的Person.Name属性到一个文本框。名称的类型为StringField。我最初将Name属性定义为String。数据绑定在值类型(如String)上非常有用。我希望StringField.Value属性是StringField的默认属性。我想在textBox中看到StringField.Value的值,而不是文本“FieldApp.StringField”。

问题2:

我希望能够使用operator =将字符串分配给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");
        }
    }
}

5 个答案:

答案 0 :(得分:10)

您可以创建一个implisit运算符重载。然后你可以从这样的字符串创建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;
    }
}

答案 1 :(得分:2)

重新数据绑定,某些绑定目标(PropertyGridDataGridView等),您可以使用TypeConverter执行此操作(请参见下文) 。不幸的是,这似乎不适用于TextBox,所以我认为你最好的选择是简单地添加一个垫片属性(如已经建议的那样):

string NameString
{
   get { return Name.Value; }
   set { Name.Value = value; } // or new blah...
}

(并绑定到NameString

过去,我使用自定义PropertyDescriptor实现来支持这一点,但是只是不值得。

无论如何,TypeConverter示例(适用于PropertyGridDataGridView):

[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);
    }
}

答案 2 :(得分:1)

您可以通过提供转换运算符来实现分配。鉴于您的类的性质,您还应该重写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();
  }
}

答案 3 :(得分:0)

您可以通过将Name属性映射到类内部的Name.Value字段来对StringField进行处理。

所以你可以像这样定义Name属性:

string Name 
{
   get { return _name.Value; }
   set { _name.Value = value; }
}

这里_name是你的StringField变量。

答案 4 :(得分:0)

无法在C#中覆盖赋值运算符。但是,您可以拥有一个属性来为您进行类型转换,并将该类转换为类