Custom Macro. How to get type of parameter of IParameter object.

Hello.

I have been trying to create some custom macro for generation some code based on properties of parameters type.
I have used next code:

var method = TextControlToPsi.GetContainingTypeOrTypeMember(context.SessionContext.Solution, control);
IParameter item = ((IMethod)method).Parameters.Where(p => p.ShortName == myArgument.GetValue()).FirstOrDefault();

And now I want to know what is type of parameter and what are properties of this type. But it is wraped by JetBrains object and I can't get it in clean way.
I found that properties are placed in next collection as item.Type.GetClassType().Fields but I still cant get clean property names.

Could you please help me with this?


Thanks.

4 comments
Comment actions Permalink

I didn't find another solution how do it only in dirty way:

var prop = item.Type.GetClassType().Fields;

foreach (var prop in props)
{
    builder.AppendLine(prop.ShortName.Substring(prop.ShortName.IndexOf('<') + 1, prop.ShortName.IndexOf('>')-1));
}


0
Comment actions Permalink

I'm not sure I follow what you're after. You mention looking for properties of the parameter type, but your code is looking at fields, and the string makes it look like you're after generic information. Can you provide a bit more context, please?

0
Comment actions Permalink

Hello Matt.
Thanks for the answer.

For example I have next class:
class User
{
  public int Id{get;set;}
  public int Room{get;set;}
}

I would like to have parameters colletion of this type   - by reflection in C# it can be made by next code: userObject.GetType().GetProperties();

But I can't get exactly this object in my macro. I have tried to get this object  from IParameter but it is wrapped by JetBrains.Resharper.Psi.IType and method GetProperty() return me properties of JetBrains object but not from object of user class.

Thanks,
Roman Dovhun

0
Comment actions Permalink

Hello.
Thanks for your remark. It helped me to found what i want.
You are right. I reuse properties method and all works.However I still can't get clear object but can do my macro without it.

                    var classType = type.GetClassType();
                    if (classType != null)
                    {
                        var props = classType.Properties;                     
                        string template = myArguments[0].GetValue();
                        foreach (var prop in props)
                        {
                            string propName = prop.ShortName;                         
                            builder.AppendLine(string.Format(template, propName));
                        }
                    }

Thanks for help.

0

Please sign in to leave a comment.