user360607
user360607

Reputation: 373

Implement similar properties within a class using a "template" in C#

I've been wondering for a time now - in C# is there a way to define a "template" for several properties within a class. Here is what I mean:

Let's assume I have the following class

class MyCLass
{
    public int  IntVal1 { get {...}; set{...} }
    public byte IntVal2 { get {...}; set{...} }
    ....
    public long IntValN { get {...}; set{...} }
}

I did not write any specific implementation in the get and set accessors but the idea is that all these properties have very similar implementations - the difference may be that they operate on different members of the class which have different types, but as a whole they all look alike.

My idea is to find a way to define some sort of (let's call it) "template" with some parameters probably that may be used to declare all these properties without the need to write the actual implementation of each and every one of them - maybe using attributes!?!

I guess what I need is similar to a C macro.

Upvotes: 6

Views: 512

Answers (4)

Warlock
Warlock

Reputation: 7471

You need a virtual protected methods in the base class. This methods will set up properties. In a child class you can inherit a base implementation or override Init/Set/Get methods and make a custom implementation.

abstract class BaseMyClass
{
    public BaseMyClass(arg1, arg2,...)
    {
        Init(arg1, arg2,...);
    }

    public int  IntVal1 { get {...}; set{...} }
    public byte IntVal2 { get {...}; set{...} }
    public byte IntVal3 
    { 
        get 
        {
            return GetIntVal3(); 
        } 
        set
        {
            SetIntVal3(value);
        }
    }

    protected void virtual Init(arg1, arg2,...)
    {
         //Init properties
    }

    protected virtual byte GetIntVal3()
    {
         //Implementation
    }

    protected virtual void SetIntVal3(value)
    {
         //Implementation
    }
}

class MyCLass : BaseMyClass
{
    public MyCLass(arg1, arg2, ...): base(arg1, arg2,...)
}

class AnotherMyCLass : BaseMyClass
{
    public MyCLass(arg1, arg2, ...): base(arg1, arg2,...)

    protected override void Init(arg1, arg2,...)
    {
         //Init properties
    }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063864

The short answer would be "no", but there are things you can do to reduce repetition. For example, consider:

private bool SetField<T>(ref T field, T value,
    [CallerMemberName] string memberName = null)
{
    if (!EqualityComparer<T>.Default.Equals(field, value))
    {
        field = value;
        var handler = PropertyChanged;
        if (handler != null) handler(this,
            new PropertyChangedEventArgs(memberName));
        return true;
    }
    return false;
}

which can be used to reduce overhead by something like:

private string bar;
public string Bar
{
    get { return bar; }
    set { SetField(ref bar, value); }
}

Upvotes: 3

Cornelius
Cornelius

Reputation: 3616

You can have a look at T4 templates and generating code from it: http://msdn.microsoft.com/en-us/library/vstudio/bb126445.aspx

If you use partial classes you can use one for the generated code and the other for the non-generated code.

Here is a nice tutorial on it: http://t4-editor.tangible-engineering.com/How-Do-I-With-T4-Editor-Text-Templates.html

Upvotes: 1

Davin Tryon
Davin Tryon

Reputation: 67326

Yes, if I understand correctly, in C# we use Generics for this:

class MyCLass<T>
{
    public T Val { get {...}; set{...} }
}

T defines the type that you want to "template".

And you then use the class like this:

var myClassInt = new MyClass<int>();
myClassInt.Val // is an integer

Upvotes: 2

Related Questions