Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Extend class by adding properties [duplicate]

Tags:

c#

oop

Is it possible in C# to extend a class not by adding only functions but properties. Ex: i have a standard DLL library I am relying on and the vendor does not want to modify it.

Already throughout the code I have used the DataCell class extensively and only now realized that I need to add an extra property to it, as creating a new extension class that inherits from this class just does not look like it would work + a lot of rewriting.

DataCell [metadata]

public class DataCell : Message
{
public int Field1;
public int Field2;
public DataCell()
{
 ..
} 
..
}

Basically I want to add a public int Flags; to this class. So I can do now without rewriting anything, (new DataCell).Flags = 0x10;

like image 724
Vans S Avatar asked Jul 12 '13 13:07

Vans S


3 Answers

First of all, you should probably reconsider your approach. But if all else fails, here is how you can sort of add a property to a sealed class:

using System;
using System.Runtime.CompilerServices;

namespace DataCellExtender
{

    #region sample 3rd party class
    public class DataCell
    {
        public int Field1;
        public int Field2;
    }
    #endregion

    public static class DataCellExtension
    {
        //ConditionalWeakTable is available in .NET 4.0+
        //if you use an older .NET, you have to create your own CWT implementation (good luck with that!)
        static readonly ConditionalWeakTable<DataCell, IntObject> Flags = new ConditionalWeakTable<DataCell, IntObject>();

        public static int GetFlags(this DataCell dataCell) { return Flags.GetOrCreateValue(dataCell).Value; }

        public static void SetFlags(this DataCell dataCell, int newFlags) { Flags.GetOrCreateValue(dataCell).Value = newFlags; }

        class IntObject
        {
            public int Value;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var dc = new DataCell();
            dc.SetFlags(42);
            var flags = dc.GetFlags();
            Console.WriteLine(flags);
        }
    }
}

Please don't do this unless you really must. Future maintainers of this code may have some strong words for you if there's a cleaner solution that you skipped in favor of this slightly hacky approach.

like image 183
dss539 Avatar answered Sep 29 '22 17:09

dss539


Well you can certainly extend a class and only add fields/properties to it (although I'd discourage the use of public fields as per your sample). However, unless other code uses your new class, the fields won't exist in the objects created. For example, if other code has:

DataCell cell = new DataCell();

then that won't have your Field1 and Field2 fields.

If every instance of the base class really should have these fields, you'd be better off working out how to change the base class rather than extending it.

If you were wondering whether you could add "extension fields" in the same way as extension methods are added (e.g. public static void Foo(this DataCell cell)) then no, that's not possible.

like image 23
Jon Skeet Avatar answered Sep 29 '22 17:09

Jon Skeet


There are two ways to add properties to an existing class

  1. Add partial class, but this won't work for you because partial classes should be in the same assembly.

  2. Inherit this class in a different class which as far I know would be a better solution for you.

And no you can't use a extension property like an extension method.

like image 27
jonni Avatar answered Sep 29 '22 15:09

jonni