



















Author:水如烟
1、CustomAttribute何时实例化?仅当查询时。
Public Class Author
Private gUser As String
Private gCanRead As Boolean
Public Property CanRead() As Boolean
Get
Return gCanRead
End Get
Set(ByVal value As Boolean)
gCanRead = value
End Set
End PropertyPublic Property User() As String
Get
Return gUser
End Get
Set(ByVal value As String)
gUser = value
End Set
End Property
End ClassPublic Module EnvironmentVars
Public CurrentUser As New Author
End Module<AttributeUsage(AttributeTargets.Method)> _
Public Class ReadPermissionAttribute
Inherits Attribute
Sub New()
Console.WriteLine("Attribute .ctor AT {0}", Now)If EnvironmentVars.CurrentUser.CanRead Then
Console.WriteLine("Please!")
Else
Console.WriteLine("Sorry!")
Throw New Exception("Sorry!")
End If
End SubProtected Overrides Sub Finalize()
Console.WriteLine("Attribute Finalize AT {0}", Now)
End Sub
End ClassPublic Class ReadInformationsPrivate Function GetDriversCount() As Integer
Dim mCount As Integer = IO.DriveInfo.GetDrives.Length
Return mCount
End Function<ReadPermission()> _
Public Function GetDriversLenth() As Integer
Return GetDriversCount()
End FunctionEnd ClassPublic Class Programs
<MTAThread()> _
Public Shared Sub Main()
Run()
End SubPrivate Shared Sub Run()
EnvironmentVars.CurrentUser.User = "LzmTW"
Dim Read As New ReadInformations
Dim mCount As Integer = Nothing
EnvironmentVars.CurrentUser.CanRead
= TrueEnvironmentVars.CurrentUser.CanRead
= FalseConsole.ReadLine()
End Sub结果是:
Read Drivers: 7
Read Drivers: 7
将
<ReadPermission()> _
Public Function GetDriversLenth() As Integer
Return GetDriversCount()
End Function
改为
<ReadPermission()> _
Public Function GetDriversLenth() As Integer
Reflection.MethodBase.GetCurrentMethod.GetCustomAttributes(False)
Return GetDriversCount()
End Function
结果是:
Attribute .ctor AT 2007-2-5 17:48:16
Please!
Read Drivers: 7
Attribute .ctor AT 2007-2-5 17:48:16
Sorry!
Read Drivers: 0
2、CustomAttribute实例何时释放?仅当GC回收时。
Private Shared Sub Run()
'...
GC.Collect()
Console.ReadLine()
End Sub
结果是:
Attribute .ctor AT 2007-2-5 17:53:41
Please!
Read Drivers: 7
Attribute .ctor AT 2007-2-5 17:53:42
Sorry!
Read Drivers: 0
Attribute Finalize AT 2007-2-5 17:53:42
Attribute Finalize AT 2007-2-5 17:53:42
3、引入CodeAccessSecurityAttribute呢?效果又不一样。简单的:
Imports System.Security
Imports System.Security.PermissionsPublic Class Author
Private gUser As String
Private gCanRead As Boolean
Public Property CanRead() As Boolean
Get
Return gCanRead
End Get
Set(ByVal value As Boolean)
gCanRead = value
End Set
End PropertyPublic Property User() As String
Get
Return gUser
End Get
Set(ByVal value As String)
gUser = value
End Set
End Property
End ClassPublic Module EnvironmentVars
Public CurrentUser As New Author
End Module<AttributeUsage(AttributeTargets.Method)> _
Public Class ReadPermissionAttribute
Inherits CodeAccessSecurityAttributePublic Sub New(ByVal action As SecurityAction)
MyBase.New(action)
End SubPublic Overrides Function CreatePermission() As System.Security.IPermission
If Not EnvironmentVars.CurrentUser.CanRead Then
Throw New Exception
End If
Return Nothing
End Function
End ClassPublic Class ReadInformationsPrivate Function GetDriversCount() As Integer
Dim mCount As Integer = IO.DriveInfo.GetDrives.Length
Return mCount
End Function<ReadPermissionAttribute(SecurityAction.Deny)> _
Public Function GetDriversLenth() As Integer
Return GetDriversCount()
End FunctionEnd ClassPublic Class Programs
<MTAThread()> _
Public Shared Sub Main()
Run()
End SubPrivate Shared Sub Run()
EnvironmentVars.CurrentUser.User = "LzmTW"
Dim Read As New ReadInformations
Dim mCount As Integer
EnvironmentVars.CurrentUser.CanRead
= TrueEnvironmentVars.CurrentUser.CanRead
= FalseConsole.ReadLine()
End Sub结果:
Read Drivers: 7
Read Drivers: 0
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。