I have a question about DSL Class Shape Properties.
I create a kind of DSL Tool. I want to have functionality a little bit like in class diagram.
I have shapes in my model. I have Class Shape with compartment part for attributes and operations. In model I h开发者_运维技巧ave attribute class and operation class.
I created "NameAndType", "Name" and "Type" property for atributtes like in book "Domain-Specific Development with Visual Studio DSL Tools. Steve Cook, Gareth Jones, Stuart Kent, Alan Cameron Wills" on 404 page.
It works great but I want to have dynamic list for parameters in operations class. Some kind of Collections property.
Now I want to create NameAndType Collections property for opeations. I want to choose how many parameters I want to use in my operation(method). That must be dynamic list (a kind of collection)
Do you know how can I do this?
Regards Adam
I resolve my problem.
I write down this on this forum. I think it might be helpful for others .A
Below is my solution:
So, I wanted to create my own Collection Editor like a property in a Domain Class (e.g. NameTypeList)
I created custom class witch have two fields (string _name, string _type) and have getters and setters for these fields. This is NameType Class . We can see code of this class below (Below is the code of this class):
[Serializable]
public class NameType
{
public NameType()
{
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _type;
public string Type
{
get { return _type; }
set { _type = value; }
}
}
This class is in the main namespace of Dsl project.
Next, we will use System.ComponentModel.Design.CollectionEditor class, so we need to add a reference System.Design to our Dsl and DslPackage projects.
So, we can create custom editor for our Property in Domain Class. We have to create editor class in custom code part in our Dsl project. We can do that using below code:
public class NameTypeEditor : System.ComponentModel.Design.CollectionEditor
{
public NameTypeEditor(Type t)
: base(t)
{
}
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
return base.EditValue(context, provider, value);
}
}
Now, we should add our custom type in Dsl project. We can do that by clicking the left mouse button in DSL Explorer on the root of tree and select “Add New External Type”.
Next we have to fill in Name as “List” and Namespace as “System.Collections.Generic”. So we have new type as generic list of objects of our custom class NameType.
After that, we have to only define new Property in our DomainClass (e.g. in DslDefinition Designer by clicking the right mouse button on Domain Class and choose Add->DomainProperty)
In Properties we have to define Name as e.g NameTypeList, choose Type as List, choose Kind as CustomStorage and set Custom Attributes like System.ComponentModel.Editor {typeof(NameTypeEditor), typeof(System.Drawing.Design.UITypeEditor)}
In the end we have to define methods for CustomStorage for our property NameTypeList that we choose in our DomainClass.
We can do that by creating partial class of this Domain Class and write GetNameTypeListValue and SetNameTypeListValue methods.
public partial class ClassElement
{
List<NameType> _nameTypeListClassParams = new List<NameType>();
public List<NameType> GetNameTypeListValue()
{
return _nameTypeListClassParams;
}
public void SetNameTypeListValue(List<NameType> value)
{
if (value != null)
_nameTypeListClassParams = value;
}
}
Now we have Collection Property NameTypeList and we can edit our list of NameType values in easy way.
Transform all, build and run it.
I solved this problem in this way. I hope this advice will help you.
精彩评论