0

I'm creating a custom class in c# and I want to add a method in a property

So, a custom method in a property for example it's like the ToString method. 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). 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.

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.

I tried this (I want to add ToInt method, ToDouble (It's more efficient)):

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:

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

Error404
  • 55
  • 8

3 Answers3

2

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();
    }
}
just-my-name
  • 465
  • 1
  • 6
  • 15
  • 1
    This is the closest the OP is going to get to what they want. The catch is that extension methods do not have access to private members of the class. – Robert Harvey May 07 '22 at 12:52
  • That's can be the key to the solution but something a "Property" (I searched) doesn't have is a type (You cannot use it). Anyway thanks you – Error404 May 07 '22 at 14:16
0

You have to use getter and setter for your property So like this:

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

your getter can be multiple lines:

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

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

Use this Link for more: Properties

  • Hello, you are very nice but I forgot to add something. I meant add a method in a property like the ToString method. I edited the first example code. Thanks – Error404 May 07 '22 at 10:31
0

You can set up an expression-bodied property TestString like this:

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