简体   繁体   English

C#使用“模板”在类中实现类似的属性

[英]C# implement similar properties within a class using a “template”

I've been wondering for a time now - in c# is there a way to define a "template" for several properties within a class. 我一直想知道 - 在c#中有一种方法可以为类中的几个属性定义“模板”。 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. 我没有在get和set访问器中编写任何特定的实现,但是想法是所有这些属性都有非常相似的实现 - 不同之处可能是它们在类的不同成员上运行,这些成员具有不同的类型,但总的来说它们都是看起来像。

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. 我想我需要的是类似于C宏。

10x in advance 提前10倍

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

Yes, if I understand correctly, in C# we use Generics for this: 是的,如果我理解正确,在C#中我们使用泛型:

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

T defines the type that you want to "template". T定义了您想要“模板”的类型。

And you then use the class like this: 然后你使用这样的类:

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

You can have a look at T4 templates and generating code from it: http://msdn.microsoft.com/en-us/library/vstudio/bb126445.aspx 您可以查看T4 templates并从中生成代码: 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 这是一个很好的教程: http//t4-editor.tangible-engineering.com/How-Do-I-With-T4-Editor-Text-Templates.html

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. 在子类中,您可以继承基本实现或覆盖Init / Set / Get方法并进行自定义实现。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM