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 Property Public Property User() As String Get Return gUser End Get Set ( ByVal value As String ) gUser = value End Set End Property End Class Public 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 Sub Protected Overrides Sub Finalize() Console.WriteLine( " Attribute Finalize AT {0} " , Now) End Sub End Class Public Class ReadInformations Private 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 Function End Class Public Class Programs < MTAThread() > _ Public Shared Sub Main() Run() End Sub Private Shared Sub Run() EnvironmentVars.CurrentUser.User = " LzmTW " Dim Read As New ReadInformations Dim mCount As Integer = Nothing EnvironmentVars.CurrentUser.CanRead = True Try mCount = Read.GetDriversLenth Catch ex As Exception End Try Console.WriteLine( " Read Drivers: {0} " , mCount) EnvironmentVars.CurrentUser.CanRead = False mCount = Nothing Try mCount = Read.GetDriversLenth Catch ex As Exception End Try Console.WriteLine( " Read Drivers: {0} " , mCount) Console.ReadLine() End Sub End Class结果是:
Read Drivers: 7Read 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:16Please!Read Drivers: 7Attribute .ctor AT 2007-2-5 17:48:16Sorry!Read Drivers: 0
2、CustomAttribute实例何时释放?仅当GC回收时。
Private Shared Sub Run() ' ... GC.Collect() Console.ReadLine() End Sub结果是:
Attribute .ctor AT 2007-2-5 17:53:41Please!Read Drivers: 7Attribute .ctor AT 2007-2-5 17:53:42Sorry!Read Drivers: 0Attribute Finalize AT 2007-2-5 17:53:42Attribute Finalize AT 2007-2-5 17:53:42
3、引入CodeAccessSecurityAttribute呢?效果又不一样。简单的:
Imports System.Security Imports System.Security.Permissions 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 Property Public Property User() As String Get Return gUser End Get Set ( ByVal value As String ) gUser = value End Set End Property End Class Public Module EnvironmentVars Public CurrentUser As New Author End Module < AttributeUsage(AttributeTargets.Method) > _ Public Class ReadPermissionAttribute Inherits CodeAccessSecurityAttribute Public Sub New ( ByVal action As SecurityAction) MyBase .New(action) End Sub Public Overrides Function CreatePermission() As System.Security.IPermission If Not EnvironmentVars.CurrentUser.CanRead Then Throw New Exception End If Return Nothing End Function End Class Public Class ReadInformations Private 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 Function End Class Public Class Programs < MTAThread() > _ Public Shared Sub Main() Run() End Sub Private Shared Sub Run() EnvironmentVars.CurrentUser.User = " LzmTW " Dim Read As New ReadInformations Dim mCount As Integer EnvironmentVars.CurrentUser.CanRead = True mCount = Nothing Try mCount = Read.GetDriversLenth Catch ex As Exception Finally Console.WriteLine( " Read Drivers: {0} " , mCount) End Try EnvironmentVars.CurrentUser.CanRead = False mCount = Nothing Try mCount = Read.GetDriversLenth Catch ex As Exception Finally Console.WriteLine( " Read Drivers: {0} " , mCount) End Try Console.ReadLine() End Sub End Class结果:
Read Drivers: 7Read Drivers: 0