简体   繁体   English

如何在属性 C# 中添加方法(如 something.ToString())

[英]How to add a method in a property C# (like something.ToString())

I'm creating a custom class in c# and I want to add a method in a property我正在 c# 中创建自定义 class,我想在属性中添加一个方法

So, a custom method in a property for example it's like the ToString method .因此,属性中的自定义方法例如ToString 方法 But I want to create a new one in a property with attributes但我想在具有属性的属性创建一个新的

(like this: (像这样:

using System;

namespace foo.Example
{
    public class foo
    { 
        object object = new object();
        string test = object.Something(); //Like ToString()
    }
}

but the ToString() method in any objects like classes, methods, properties , etc).但 ToString() 方法在任何对象中,如类、方法、属性等)。 I want to add a method into a property like what I said above with the object with the.ToString() method in a property and anything.我想将一个方法添加到属性中,就像我上面所说的 object 和属性中的 .ToString() 方法以及任何东西一样。

I actually tried this: How to create a custom attribute in C# but it doesn't work, I tried everything but there are not many examples in Google and some are outdated.我实际上试过这个: How to create a custom attribute in C#但它不起作用,我尝试了一切,但谷歌中的例子不多,有些已经过时了。

I tried this (I want to add ToInt method, ToDouble (It's more efficient)):我试过这个(我想添加 ToInt 方法,ToDouble(效率更高)):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Winforms.Something.Attributes
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class PropertyToValueAttribute : Attribute
    {
        public PropertyToValueAttribute() { }

        public int ToInt()
        {
            object value = 0;
            Type type = this.GetType();
            IList<PropertyInfo> props = new List<PropertyInfo>(type.GetProperties());
            foreach (PropertyInfo prop in props)
            {
                value = prop.GetValue(this, null);
            }

            return Convert.ToInt32(value);
        }

        public double ToDouble()
        {
            object value = 0;
            Type type = this.GetType();
            IList<PropertyInfo> props = new List<PropertyInfo>(type.GetProperties());
            foreach (PropertyInfo prop in props)
            {
                value = prop.GetValue(this, null);
            }

            return Convert.ToDouble(value);
        }
    }
}

I did that in the class where I want to use the property:我在我想使用该属性的 class 中这样做了:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Winforms.Something.Attributes;

namespace Winforms.Something
{
    [Serializable]
    public class AttributedClass
    {
        private double _propexample;
        [PropertyToValue]
        public double Property
        {
            get { return _propexample; }
            set { _propexample = value; }
        }

Thanks for your help谢谢你的帮助

Maybe I don't understand your question but I have a feeling that extension method might be what you need.也许我不明白你的问题,但我觉得扩展方法可能是你所需要的。

public static class Extensions {
    public static void YourTypeExtension(this YourType self) {
        // your logic
    }
}

Then you can use it as following:然后你可以使用它如下:

public class A {
    public void Method() {
        var yourType = new YourType();
        yourType.YourTypeExtension();
    }
}

You have to use getter and setter for your property So like this:你必须为你的财产使用 getter 和 setter 所以像这样:

public class foo
{
  public object Test = new();
  public string TestString { get => Test.ToString(); }
}

your getter can be multiple lines:你的 getter 可以是多行:

public class foo
{
  public object Test = new();

  public string TestString
  {
    get
    {
        return Test.ToString();
    }
  }
}

Use this Link for more: Properties使用此链接了解更多信息: 属性

You can set up an expression-bodied property TestString like this:您可以像这样设置表达式主体属性 TestString :

public class foo
{
    public object Test = new();

    public string TestString => Test.ToString();
}

This is the equivalent of the TestString property only having a getter and no setter这相当于只有 getter 而没有 setter 的 TestString 属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM