Hi!
I have wrote next code template:
-----------------------------------------------
// auto declaration for «Paste»
«ForEach(public property in clipClass, #InitializeField#)»
-----------------------------------------------
and #InitializeField#
-----------------------------------------------
«?Get(itemName)» = «Field(value)»default(«?Get(itemType)»); // «?Get(itemType)» «?Get(itemName)»
-----------------------------------------------
And try to run it on class , for example EventArgs
I've try both - Class Name and variable of this class - all the times I've got empty result.
So, I have two questions:
- Is it possible to run this template for any class? I've try to run example with "this" instead of "public property" in first template and it's works like charm.
- Is if possible to iterate recursively class properties in foreach statement and run template for Icollection public properties? I need some reflection functionality inside template - if yes - can you please show an example how to do this?
Hi Aleksander,
Your template is absolutely correct. It looks like that you have found a bug in our template engine. This template for some reason can be applied only once after the Visual Studio startup. We will look into this problem and inform you about our progress.
Currently the template engine does not support recursive iteration across templates. We will discuss the possibility of implementing this functionality in the future.
Can you please also provide using Foreach with block, itemElement scope.
Also - can I iterate properties of <<Field>> type?
I have something like that:
----- my template where I've put class name
internal class «BlockAnchor»«Link(Entity)»«Caret»Service : Service<«Link(Entity)», pgiEFLibExampleConnection>, I«Link(Entity)»Service
{
public override void InitializeNew(ref «Link(Entity)» elem)
{
base.InitializeNew(ref elem);
// here - I want to iterate all public properties of type given in a «Link(Entity)» by <<ForEach>> statement
// like «ForEach(public property in --->>>«Link(Entity)» <<<----- , #InitializeField#)»
}
}
What I need to put instead of --->>>«Link(Entity)» <<<-----?
Is it possible to do with CodeRush?
As the template is already expanded, CodeRush can't determine which type you typed after that. In this case, I suggest that you use the clipClass keyword in the ForEach command and copy the name of the necessary class before expanding the template.
Can I use <<FinalTarget>> for this?
Can you please explain how to use itemElement scope?
Yes, you can use the FinalTarget but in this case, you should use at least one Field (for example «FieldStart»«Link(Entity)»«FieldEnd»).
The ItemElement can be used to iterate by the current element. See to the example:
startTemplate «ForEach(namespace in file,#ProcessNamespace#) #ProcessNamespace# «ForEach(class in itemElement,#ProcessClass#) #ProcessClass# «ForEach(member in itemElement,#ProcessMember#) #ProcessMember# // «?Get(itemName)»
I apologize for misleading you. It turns out there is the possibility of creating recursive templates.I have create a separate ticket to describe more detail on how to create recursive templates to help other users to easily find this information:
Templates - How to create recursive templates with conditions