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
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