A simple template base system of macros for Visual Studio that can be executed in design time
Install as a NuGet package or VSIX. Verify your Roslyn integration option in case you are using R#.
Install as a nuget package
Install as a NuGet package and check Enable support for Roslyn analyzers, code fixes and rulesets
in Settings.
To facilitate usage of CsharpMacros
I created a couple of Resharper LiveTemplates. You can read more about that on the snippets sub-page.
Every macro consists of the following parts
Macro Header
in the following formatmacros.macro_function(macro_params)
.Macro Template
- a template of code that will be repeated for every element returned by the macro function. The template can contain placeholders in the following format${attribute_name}
that will be replaced with a given attribute value of the element returned by the macro function.macro_function
represents one of the predefined functions that generate input data for the template.macro_params
is the input for macro function.- The placeholder that will be replaced with the value of the given attribute. Placeholders can also contain filters that transform the attribute value. Syntax for placeholder with filter
${attribute_Name | filter_name}
. The filters can be combined together by chaining them with|
operator. Currently available filters:lowercase
,uppercase
,pascalcase
,camelcase
,snakecase
.
Description: Returns a list of properties of given type accepted as the parameter.
Parameters: Type name. For types from other projects/assemblies include also the namespace.
Attributes:
name
- name of the propertytype
- type of the propertytypeLong
- type of the property. For primitives fromSystem
namespace, holds the full name (example:int
->Int32
)
Examples:
// macros.properties(SampleType)
// Console.WriteLine("Property '${name}' of type '${type}'")
Description: Returns a list of methods of a given type accepted as the parameter.
Parameters: Type name. For types from other projects/assemblies include also the namespace.
Attributes:
name
- name of the methodreturnType
- name of the return typereturnTypeLong
- name of the return type. For primitives fromSystem
namespace, holds the full name. Example:int
->Int32
)signature
- a complete method signature. Example:int Calculate(int a, int b)
returnOperator
- empty string forvoid
methods, otherwisereturn
keywordparameters
- comma separated list of parameters wrapped in parenthesis. Example:(a, b, c)
genericParameters
- comma separated list of generic type parameters wrapped in angle brackets. Example:<T1, T2, T3>
paramNameX
- name of the parameter. X marks the number of the parameter.paramTypeX
- name of the parameter type. X marks the number of the parameter.paramTypeLongX
- name of the parameter type. X marks the number of the parameter. For primitives fromSystem
namespace, holds the full name (example:int
->Int32
)
Examples:
// macros.methods(SampleType)
// Console.WriteLine("Method name: ${name}")
// Console.WriteLine("\t Return type: ${returnType}")
// Console.WriteLine("\t First parameter name: ${paramName1}")
// Console.WriteLine("\t First parameter type: ${paramType1}")
Description: Returns a list of types that implement a given interface accepted as the parameter. Parameters: Interface name. For interfaces from other projects/assemblies include also the namespace.
Attributes:
name
- the name of the class that implements the given interfaceinterface
- for a non-generic interface, this is the same as the parameter of the macro function. For generic interface contains the name of the interface with generic parameter values.
Examples:
// macros.implement(ISampleInterface<>)
// Console.WriteLine("Interface ${interface} is implemented by ${name}")
Description: Returns a list of types that inherit from a given class accepted as the parameter.
Parameters: Type name. For types from other projects/assemblies include also the namespace.
Attributes:
name
- the name of the type that inherits from a given classbased
- for generic non-generic base class this is the same as the parameter of the macro function. For generic base class contains the name of the class with generic parameter values.
Examples:
// macros.derived(BaseClass<>)
// Console.WriteLine("Class ${name} inherits from ${based}")
Description: Returns values from the predefined list provided as a macro parameter.
Parameters: List of comma-separated values or list of comma-separated tuples
Attributes:
value
- for simple listvalueX
- for list of tuples. X marks the number of the value inside the tuple
Examples:
Simple list:
// macros.values(banana, strawberry, apple)
// Console.WriteLine("Fruit ${value}")
List of tuples:
// macros.values((banana, yellow), (strawberry, red), (apple, green))
// Console.WriteLine("${value1} fruit has ${value2} color.")
Description: Returns values from the predefined range.
Parameters: The begin and end of the range
Attributes:
index
- current counter valuefrom
- The begin of the rangeto
- The end of the range
Examples:
// macros.range(10, 15)
//Console.WriteLine("Value ${index} from range [${from} - ${to}");
Description: Returns a list of enum values
Parameters: Enum type. For types from other projects/assemblies include also the namespace.
Attributes:
name
- the name of enum option
Examples:
// macros.enum(SampleEnumType)
//Console.WriteLine("Option: ${name}");