Hi all,
I'm trying to write an Add-on in C# to modify attributes applied to the
assembly itself, e.g. (in AssemblyInfo.cs)
[assembly: AssemblyVersion("1.0.*")]
What's the best approach? I'd like to use CodeModel, but I'm having
difficulties.
I'm enumerating through the collection of a project's CodeElements to find
AssemblyVersionAttribute.
Once I've found it, I'm trying to retrieve .StartPoint of it but getting a
COM exception 0x80004005 Unspecified error.
Also, I'm searching for the class recursively in my GetClass() method, and
it returns too many classes, even those that don't exist in my code.
Any help is greatly appreciated.
Here's my code:
foreach (CodeElement codeElement in project.CodeModel.CodeElements)
{
if (codeElement is CodeNamespace || codeElement is CodeClass)
{
GetClass(codeElement, ref output);
}
....
private void GetClass(CodeElement codeElement, ref string output)
{
if (codeElement is CodeClass)
{
output += ", " + codeElement.FullName;
CodeClass cc = (CodeClass)codeElement;
if (cc.FullName ==
typeof(System.Reflection.AssemblyVersionAttribute).FullName)
{
TextPoint startPoint = cc.StartPoint;
}
}
CodeElements subElements = null;
if (codeElement is CodeClass)
{
subElements = ((CodeClass)codeElement).Members;
}
else if (codeElement is CodeNamespace)
{
subElements = ((CodeNamespace)codeElement).Members;
}
if (subElements != null)
{
foreach (CodeElement subElement in subElements)
{
if (subElement is CodeClass || subElement is CodeNamespace)
GetClass(subElement, ref output);
}
}
}
Chetan Chudasama - 29 Sep 2004 02:14 GMT
Oleg,
What version of VS are you using: VS 2003 or Whidbey Beta 1?
The Project.CodeModel is most useful for VC++ projects (see documentation
at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsintro7/ht
ml/vxlrfcodemodelproperty.asp). If your addin will work on C# or VB
projects then you need to use FileCodeModel object.
Even when you use the FileCodeModel, the support is not very good in VS
2003 and you may still not be able to get the AssemblyAttributes in the
AssemblyInfo file.
The CodeModel support has been greatly improved in Whidbey and coding up
what you are trying to do is very easy and straight forward.
- Chetan [Extensibility QA, MSFT]