6

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.

0

4 Answers 4

3

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); }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's what I ended up with. Just thought I'd check the alternatives.
2

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

3 Comments

As far as i understood he wants to "template" the implementation, not the type definitions.
@LuisFilipe "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." - not sure if this means generics or not.
It is a Luis said. To put it in other words: I'm looking for a way to create a base implementation for all properties in my class that are "look-alikes" and then define each of those properties as based on that "base property" template.
1

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

2 Comments

Will have to look at the articles in detail, but my initial understanding is that those T4 templates can be used to automatically create the necessary code (based on a predefined template). What I meant though was whether there is a support in C# itself for constructing properties (in my case) with similar implementation by defining a common "template" and "attaching" it to each of those properties.
A T4 template can define the common "template" and be run on any kind of config file which can then be the "attaching" part. It is how Entity Framework generate the code needed based on certain configs for classes. T4 is the suggested way of creating code/do the jobs of macros and is found in many technologies/frameworks. Perhaps if you could give more specific details on the kind of property implementation you want to achieve?
0

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.