Add In does not work on VB.NET project but does on C#

M

Mario Sobral

Hi !

I have written an Add-In to use in VS.NET 2003.

The Add-In asks the user for a public key on a small form,
and then
basically goes through a selected project, looking for
public classes,
and when finds one, it checks if the attribute
StrongNameIdentityPermissionAttribute exists.

If so, it should update it to a new public key specified
before. If
not, it should create the attribute.

I am developing this add-in in VB.NET (don't ask, current
company
policies... I usually code in C#!). When I apply the add-
in to a C#
project, it works fine. When I applied it to a VB.NET
project, it
fails. After some searching and head scratching, I found
out that the
AddAttribute function of the CodeClass is not implemented
for VB.NET
:-(. So I went and tried it by "hand", by writing the
attribute
string. Doesn't work either, showing the following error:

Error processing CodeItem: ExceptionsLog --> class being
processed at
the time of the error
Error in ProcessElem[ExceptionsLog]: Not implemented

By stepping through the code, the exception occurs on the
following
line:

ed = CurrentClass.GetStartPoint.CreateEditPoint


This is the method where it all goes wrong:

-----------------------------------------------------------
--------------
Private Shared Sub ProcessElem(ByVal Elem As CodeElement,
ByVal
AttributeName As String, ByVal Key As String, ByVal
Language As
ProjectLanguage)
'-- If already found a class element,
insert/update attribute
Dim CurrentClass As CodeClass = CType(Elem,
CodeClass)
If CurrentClass.Access.vsCMAccessPublic Then
Dim AttFound As Boolean = False
Try
If CurrentClass.Attributes.Count > 0 Then
'-- For the current class, see if the
attribute
StrongNameIdentityPermissionAttribute exists and act
accordingly
For Each CurrentAttribute As
CodeAttribute In
CurrentClass.Attributes
'-- If the attribute already
exists, change it
to the new public key
If CurrentAttribute.Name =
AttributeName Then
Dim NewAttribute As
System.Security.Permissions.StrongNameIdentityPermissionAtt
ribute
NewAttribute = CType
(CurrentAttribute,
System.Security.Permissions.StrongNameIdentityPermissionAtt
ribute)
NewAttribute.Action =
Security.Permissions.SecurityAction.LinkDemand
NewAttribute.PublicKey = Key
CurrentAttribute = NewAttribute
Log.Write("Updated public key
in class: "
& CurrentClass.Name, NewKey.DTE.Solution.FullName & ".log")
AttFound = True
End If
Next
End If
'-- If no
StrongNameIdentityPermissionAttribute was
found, create one and add it to the class
If Not AttFound Then
'-- According to MS, the AddAttribute
function is
not implemented yet for VB.NET
'Dim NewAttribute As
System.Security.Permissions.StrongNameIdentityPermissionAtt
ribute
'NewAttribute =
CurrentClass.AddAttribute
("StrongNameIdentityPermissionAttribute", "")
'NewAttribute.Action =
Security.Permissions.SecurityAction.LinkDemand
'NewAttribute.PublicKey = Key
Dim ed As EditPoint
'-------- ERROR HAPPENS ON THE NEXT LINE ---------------
ed =
CurrentClass.GetStartPoint.CreateEditPoint
If Language = ProjectLanguage.VBNET
Then
ed.Insert("<" & AttributeName &
"(SecurityAction.LinkDemand, PublicKey := "" & Key & "")>
_" & vbCrLf)
ElseIf Language.CSHARP Then
ed.Insert("[" & AttributeName &
"(SecurityAction.LinkDemand, PublicKey = """ & Key
& """)] " & vbCrLf)
End If
Log.Write("Added public key to
class: " &
CurrentClass.Name, NewKey.DTE.Solution.FullName & ".log")
End If
Catch ex As Exception
Dim Message As String = "Error in
ProcessElem[" &
Elem.Name & "]: "
Throw New Exception(Message & ex.Message)
End Try
End If
End Sub
-----------------------------------------------------------
--------------

Thanks in advance for any help !


Mario Sobral
 
B

Bharat Patel [MSFT]

Mario,

You need to pass a Constant in the GetStartPoint function. Look at the vsCMPart
constants in the help to find out what part you need.

Your code should look like the following:

CurrentClass.GetStartPoint(vsCMPart.vsCMPartAttributes).CreateEditPoint()


Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
M

Mario Sobral

Bharat,

THanks for your reply, I was only able to go back to this project now.
I have tried your recommendation, using the following code:

(...)
Dim ed As EditPoint
ed = CurrentClass.GetStartPoint(vsCMPart.vsCMPartAttributes).CreateEditPoint
(...)


But it still gives me the "Not implemented" error :-(

I haven't been able to solve this problem, and it's so strange to have
errors such as these. Something done in VB.NET, when applied to C#
project works, but when applied to VB.NET project doesn't work :-S

Mario Sobral
 
B

Bharat Patel [MSFT]

Hi Mario,

I checked Object Browser for CodeClass interface and did not see GetStartPoint
method or property.
You can try StartPoint property of CodeClass and then call CreateEditPoint.

I am sure you already have the reference to EnvDTE.dll in your project. So if
you open the Object Browser, you will see all the properties and methods of the
CodeClass.
Go through it and you will figure out what to do.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
M

Mario Sobral

Yes, I did that and it worked, thanks !!!!!!!! What a simple solution
for such a problem :)

Nevertheless, the GetStartPoint shows in Intelissense as well as the
StartPoint property.

I now have another small prblem, but I'll post it separatly.

Thanks again for your time and the helpful suggestions, Bharat.

Mario Sobral
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top