How to inject method to auto property in .Net Framework

前端 未结 3 1447
夕颜
夕颜 2021-01-15 15:15

I have some class Foo with many properties:

public class Foo
{
    public int Property1 { get; set; }

    public int Property2 { get; set; }

    public int         


        
3条回答
  •  太阳男子
    2021-01-15 16:08

    You can use an extension of the int class here. Or whatever data type your getter/setter properties are.

    For example

    public class Foo
    {
        public int Property1 { get; set; }    
        public int Property2 { get; set; }    
        public int Property3 { get; set; }
    }
    

    The extension method would look like this

    public static class IntExtension
    {
        public static void SomeMethod(this int property)
        {
            // ...
        }
    }
    

    See the following article to use it with .NET 2.0. Requires that you use a VisualStudio that supports C# 3.0 but it will still work with the output framework as C# 2.0

    Extension Method in C# 2.0

提交回复
热议问题