惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
S
Secure Thoughts
I
Intezer
Project Zero
Project Zero
Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Full Disclosure
P
Proofpoint News Feed
T
The Exploit Database - CXSecurity.com
人人都是产品经理
人人都是产品经理
博客园_首页
J
Java Code Geeks
Recorded Future
Recorded Future
K
Kaspersky official blog
GbyAI
GbyAI
S
Schneier on Security
The Cloudflare Blog
Spread Privacy
Spread Privacy
C
Cisco Blogs
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Know Your Adversary
Know Your Adversary
T
Tenable Blog
A
Arctic Wolf
Blog — PlanetScale
Blog — PlanetScale
H
Hacker News: Front Page
The Last Watchdog
The Last Watchdog
O
OpenAI News
Last Week in AI
Last Week in AI
B
Blog RSS Feed
T
Troy Hunt's Blog
G
GRAHAM CLULEY
N
Netflix TechBlog - Medium
Vercel News
Vercel News
量子位
The Register - Security
The Register - Security
Google Online Security Blog
Google Online Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
U
Unit 42
Security Archives - TechRepublic
Security Archives - TechRepublic
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic

博客园 - 水如烟(LzmTW)

.NET的变量在代码集中是不安全的 HOW TO:获取硬盘物理序列号(VB.NET) 一个创建快捷方式类 一个简单的CodeAccessPermission生成器 HOW TO:端口打印(比较粗糙) 标记法定义和创建数据库(四) 标记法定义和创建数据库 Sql2005数据类型与Framework类型的对应关系 HOW TO:重启程序(WinForm) HOW TO:设置默认打印机 HOW TO:避免“将COM对象与其基础RCW分开后不能再使用该对象”错误 HOW TO:控制是否允许用户退出ExcelApplication的Workbook 树和自联表(五) 树和自联表(六) 树和自联表(四) 树和自联表(一) 行政区划数据数据库的设计(脚本) 行政区划数据数据库的设计(七) 行政区划数据数据库的设计(六)
认识一下Attribute
水如烟(LzmTW) · 2007-02-05 · via 博客园 - 水如烟(LzmTW)

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 

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

= 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: 7
Read Drivers: 0