[C#] Is it possible to automatically generate attributes on properties?

Hello!

I searched and did not think that this was asked already.

I learned about 'Live Templates', its very cool!

Basically, I have to add attributes to every single property on pre-existing classes.

Scenario - For example:

I have 50 classes, each with 10 properties.

Current abilities:

Currently, I can create a 'Live Template', and going to each individual property, I can quickly generate the attribute.  This takes time for may attributes.

Expected Goal:

I want to go to a class, and automatically create attributes on all 10 properties.  

Question:

Is it possible to automatically add attributes to two or more properties on a class?

 

Thanks!

 

 

0
4 comments

Hi Jeff Ward

Thank you for the example. I'm not sure this is possible exact as you want. 

However, as a possible workaround, you can use the Multiple carets feature. So you can easily obtain a sample code like this: 

1

Hi Jeff Ward

Could you please add a small sample of code that illustrates what you have and what you want to obtain? 

Thanks! 

0

Hello Alexandra Guk!


Of course! Hopefully this helps:

Link to code: https://codebeautify.org/cs/42915e

````csharp

// ~~~ Current configured 'Live Templates' ~~~
// Shortcut: opp
// Generated Template: [OpenApiProperty(Description = "SmartTypeByRiderHere")]

 

// This is the target class
// I would like to automatically apply the 'Live Template' to each of these properties within the type 'SampleClass'

// -- Currently, I can go to each property and type 'opp' which will generate the Attribute - I am wondering if there is a way how I can avoid writing 'opp' 6+ times and can 'automatically' apply the Live Template attribute to all properties on the target type - in 1 action (instead of 6+ actions)

public class SampleClass
{
    public string FirstProperty { get; set; }

    public string SecondProperty { get; set; }

    public string ThirdProperty { get; set; }

    public string FourthProperty { get; set; }

    public string FifthProperty { get; set; }

    public string SixthProperty { get; set; }
}

// Expected result:


public class SampleClass
{
    [OpenApiProperty(Description = "FirstProperty")]
    public string FirstProperty { get; set; }

    [OpenApiProperty(Description = "SecondProperty")]
    public string SecondProperty { get; set; }

    [OpenApiProperty(Description = "ThirdProperty")]
    public string ThirdProperty { get; set; }

    [OpenApiProperty(Description = "FourthProperty")]
    public string FourthProperty { get; set; }

    [OpenApiProperty(Description = "FifthProperty")]
    public string FifthProperty { get; set; }

    [OpenApiProperty(Description = "SixthProperty")]
    public string SixthProperty { get; set; }
}

````

 

Please let me know if I can provide any other examples! Thanks for the help and quick response!

0

Alexandra Guk


Thank you! This is very helpful! I appreciate it!

 

We can close this thread, I do not see a close button - so I wanted to mention it to you. 

 

Thanks!

0

Please sign in to leave a comment.