Does C# have extension properties?

Does C# have extension properties?

For example, can I add an extension property to DateTimeFormatInfo called ShortDateLongTimeFormat which would return ShortDatePattern + " " + LongTimePattern ?


No they do not exist in C# 3.0 and will not be added in 4.0. It's on the list of feature wants for C# so it may be added at a future date.

At this point the best you can do is GetXXX style extension methods.


No, they don't exist.

I know that the C# team was considering them at one point (or at least Eric Lippert was) - along with extension constructors and operators (those may take a while to get your head round, but are cool...) However, I haven't seen any evidence that they'll be part of C# 4.

EDIT: They didn't appear in C# 5, and as of July 2014 it doesn't look like it's going to be in C# 6 either.

Eric Lippert, the Principal Developer on the C# compiler team at Microsoft thru November 2012, blogged about this in October of 2009:

  • Why No Extension Properties? – Fabulous Adventures In Coding

  • For the moment it is still not supported out of the box by Roslyn compiler ...

    Until now, the extension properties were not seen as valuable enough to be included in the previous versions of C# standard (ie 5 and 6).

    But it will ...

    There is an extension members item in the C# 7 work list so it may be supported in the near future. The current status of extension property can be found on Github under the related item.

    However, there is an even more promising topic which is the "extend everything" with a focus on especially properties and static classes or even fields.

    Moreover you can use a workaround

    As specified in this article, you can use the TypeDescriptor capability to attach an attribute to an object instance at runtime. However, it is not using the syntax of the standard properties.
    It's a little bit different from just syntactic sugar adding a possibility to define an extended property like
    string Data(this MyClass instance) as an alias for extension method
    string GetData(this MyClass instance) as it stores data into the class.

    I hope that C#7 will provide a full featured extension everything (properties and fields), however on that point, only time will tell.

    And feel free to contribute as the software of tomorrow will come from the community.

    Update: August 2016

    As dotnet team published what's new in C# 7.0 and from a comment of Mads Torgensen:

    Extension properties: we had a (brilliant!) intern implement them over the summer as an experiment, along with other kinds of extension members. We remain interested in this, but it's a big change and we need to feel confident that it's worth it.

    It seems that extension properties and other members, are still good candidates to be included in a future release of Roslyn, but maybe not the 7.0 one.

    Update: May 2017

    The extension members has been closed as duplicate of extension everything issue which is closed too. The main discussion was in fact about Type extensibility in a broad sense. The feature is now tracked here as a proposal and has been removed from 7.0 milestone .

    Update: August, 2017 - C# 8.0 proposed feature

    While it still remains only a proposed feature, we have now a clearer view of what would be its syntax. Keep in mind that this will be the new syntax for extension methods as well:

    public interface IEmployee 
    {
        public decimal Salary { get; set; }
    }
    
    public class Employee
    {
        public decimal Salary { get; set; }
    }
    
    public extension MyPersonExtension extends Person : IEmployee
    {
        private static readonly ConditionalWeakTable<Person, Employee> _employees = 
            new ConditionalWeakTable<Person, Employee>();
    
    
        public decimal Salary
        {
            get 
            {
                // `this` is the instance of Person
                return _employees.GetOrCreate(this).Salary; 
            }
            set 
            {
                Employee employee = null;
                if (!_employees.TryGetValue(this, out employee)
                {
                    employee = _employees.GetOrCreate(this);
                }
                employee.Salary = value;
            }
        }
    }
    
    IEmployee person = new Person();
    var salary = person.Salary;
    

    Similar to partial classes, but compiled as a separate class/type in a different assembly. Note you will also be able to add static members and operators this way. As mentioned in Mads Torgensen podcast, the extension won't have any state (so it cannot add private instance members to the class) which means you won't be able to add private instance data linked to the instance . The reason invoked for that is it would imply to manage internally dictionaries and it could be difficult (memory management, etc...). For this, you can still use the TypeDescriptor / ConditionalWeakTable technique described earlier and with the property extension, hides it under a nice property.

    Syntax is still subject to change as implies this issue. For example, extends could be replaced by for which some may feel more natural and less java related.

    链接地址: http://www.djcxy.com/p/17446.html

    上一篇: 扩展方法可以应用于接口吗?

    下一篇: C#有扩展属性吗?