Re: Using the API to Create ISerializable Calls

The API you need is located in JetBrains.ReSharper.Psi.dll (PSI - is abbreviation
from Program Structure Interface).

First of all you need to fetch an object representing a class (IClass) and
explore its fields (IField's).
Create an action (see openapi use samples in the installation) and fetch
a class:

IClass aClass = context.GetData(DataConstants.DECLARED_ELEMENT) as IClass;

Then (when aClass is not null) fetch its fields:

IField[] fields = aClass.Fields;

To generate the method use:

ICSharpElementFactory factory = aClass.GetManager().GetElementFactory();
IMethodDeclaration methodDecl = (IMethodDeclaration)factory.CreateTypeMemberDeclaration(text);


This creates a method declaration which is not located in any class. To insert
the declaration you need fetch a declaration of the class.
To fetch declarations of the class:

IDeclaration[] declarations = aClass.GetDeclarations();

There can be multiple declarations in case of partial class. You have to
choose one to generate method into. This adds some complexity, so let's skip
it for first iteration (choose the first declaration).
NOTE: never assume that array of declarations is non-empty. Fetching declarations
can fail by a number of reasons. Just handle this case gracefully by exiting
the action.

Check that the declaration is C# class declaration:

JetBrains.ReSharper.Psi.CSharp.IClassDeclaration classDecl = declarations[0]
as JetBrains.ReSharper.Psi.CSharp.IClassDeclaration;

And now insert method:

methodDecl = (IMethodDeclaration)classDecl.AddClassMemberDeclaration(methodDecl);

And reformat it according to code style settings:

ICodeFormatter formatter = CodeFormatterProvider.GetInstance (solution).GetFormatter
(myClassDeclaration.Language);
formatter.Format(methodDecl, formatter.GeneratorProfile);

Valentin Kipiatkov
Chief Scientist, Vice President of Product Development
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

I can't find anything on using the API... can you get me started, or
point me where I need to go?

I'd like to use the Resharper API to build GetObjectData() methods
automatically for any specified classes that implement ISerializable.

I'd like to show a field selector, displaying all the fields of the
class, allow me to check these off, and then create the method with al
lthe info.AddValue calls for the selected fields.

info.AddValue ("fieldName", fieldName);

Also, creating the Serializable COnstructor, doing the same in
reverse:

fieldName = info.GetInt ("fieldName");

Any help would be greatly appreciated.



2 comments

Where do I find the samples you meantioned?




"Valentin Kipiatkov (JetBrains)" <valentin@jetbrains.com> wrote in message
news:3fdb29a6952058c7b294e2902229@news.intellij.net...

The API you need is located in JetBrains.ReSharper.Psi.dll (PSI - is
abbreviation from Program Structure Interface).

>

First of all you need to fetch an object representing a class (IClass) and
explore its fields (IField's).
Create an action (see openapi use samples in the installation) and fetch a
class:

>

IClass aClass = context.GetData(DataConstants.DECLARED_ELEMENT) as
IClass;

>

Then (when aClass is not null) fetch its fields:

>

IField[] fields = aClass.Fields;

>

To generate the method use:

>

ICSharpElementFactory factory = aClass.GetManager().GetElementFactory();
IMethodDeclaration methodDecl =
(IMethodDeclaration)factory.CreateTypeMemberDeclaration(text);

>
>

This creates a method declaration which is not located in any class. To
insert the declaration you need fetch a declaration of the class. To fetch
declarations of the class:

>

IDeclaration[] declarations = aClass.GetDeclarations();

>

There can be multiple declarations in case of partial class. You have to
choose one to generate method into. This adds some complexity, so let's
skip it for first iteration (choose the first declaration).
NOTE: never assume that array of declarations is non-empty. Fetching
declarations can fail by a number of reasons. Just handle this case
gracefully by exiting the action.

>

Check that the declaration is C# class declaration:

>

JetBrains.ReSharper.Psi.CSharp.IClassDeclaration classDecl =
declarations[0] as JetBrains.ReSharper.Psi.CSharp.IClassDeclaration;

>

And now insert method:

>

methodDecl =
(IMethodDeclaration)classDecl.AddClassMemberDeclaration(methodDecl);

>

And reformat it according to code style settings:

>

ICodeFormatter formatter = CodeFormatterProvider.GetInstance
(solution).GetFormatter (myClassDeclaration.Language);
formatter.Format(methodDecl, formatter.GeneratorProfile);

>

Valentin Kipiatkov
Chief Scientist, Vice President of Product Development
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>> I can't find anything on using the API... can you get me started, or
>> point me where I need to go?
>>
>> I'd like to use the Resharper API to build GetObjectData() methods
>> automatically for any specified classes that implement ISerializable.
>>
>> I'd like to show a field selector, displaying all the fields of the
>> class, allow me to check these off, and then create the method with al
>> lthe info.AddValue calls for the selected fields.
>>
>> info.AddValue ("fieldName", fieldName);
>>
>> Also, creating the Serializable COnstructor, doing the same in
>> reverse:
>>
>> fieldName = info.GetInt ("fieldName");
>>
>> Any help would be greatly appreciated.
>>
>



0

Hello Eric,

Where do I find the samples you meantioned?

You can dig for them in ".\VS2005\PluginSamples\" under the ReSharper installation
folder (normally something like "C:Program Files\JetBrains\ReSharper\.").

Thanks,
Andrey Simanovsky.


0

Please sign in to leave a comment.