문제

I'm making some experiments with Visual Studio's CodeModel. I tried creating a file with two namespaces in there through VS's Code Model:

CodeModel codeModel = projItem.ContainingProject.CodeModel;
codeModel.AddNamespace("Namespaces.FirstNamespace", "namespacestest.cs");
codeModel.AddNamespace("Namespaces.SecondNamespace", "namespacestest.cs");

the output of such code is:

//file namespacestest.cs
namespace Namespaces.FirstNamespace {
}
namespace Namespaces.SecondNamespace {
}

Which is almost what I'm looking for. The only catch here is that I'd like to control the spacing: having at least one line between the first and the second namespace. Does by chance anyone know of a way of achieving this?

This is what I want:

//file namespacestest.cs
namespace Namespaces.FirstNamespace {
}

namespace Namespaces.SecondNamespace {
}

Thanks

도움이 되었습니까?

해결책

The statement codeModel.AddNamespace will return you an object of CodeNamespace. you can find the EditPoint of the namespace using the your first namespace and then insert a line like this.

CodeNamespace yournamespace = codeModel.AddNamespace(....);
EditPoint2 endEditPoint = yournamespace.GetEndPoint(vsCMPart.vsCMPartWhole).CreateEditPoint();
endEditPoint.InsertNewLine(1);

I am not sure whether this will put the line at the place you want but the idea is to get the EditPoint and then use it to insert the line.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top