Skip to content

Instantly share code, notes, and snippets.

@mk3008
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save mk3008/1570bb04c2f212ab6212 to your computer and use it in GitHub Desktop.

Select an option

Save mk3008/1570bb04c2f212ab6212 to your computer and use it in GitHub Desktop.
Attribute.Inheritedと、MemberInfo.GetCustomAttributes の関係
Module Module1
Sub Main()
Dim log As Action(Of Func(Of Boolean)) =
Sub(f)
Dim cnt As Integer = 100000
Dim sw As New Stopwatch
sw.Start()
For i As Integer = 1 To 100000
f.Invoke()
Next
sw.Stop()
Console.WriteLine(String.Format("{0:n4} msec", sw.ElapsedMilliseconds / cnt))
End Sub
Dim p As Func(Of Boolean) = Nothing
p = Function() GetType(TestBase).GetCustomAttributes(GetType(InheritedAttribute), True).Any
Console.WriteLine(String.Format("通常クラス-Attribute.Inherited=True -GetCustomAttributes(True) => {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(TestBase).GetCustomAttributes(GetType(InheritedAttribute), False).Any
Console.WriteLine(String.Format("通常クラス-Attribute.Inherited=True -GetCustomAttributes(False)=> {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(TestBase).GetCustomAttributes(GetType(NotInheritedAttribute), True).Any
Console.WriteLine(String.Format("通常クラス-Attribute.Inherited=False-GetCustomAttributes(True) => {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(TestBase).GetCustomAttributes(GetType(NotInheritedAttribute), False).Any
Console.WriteLine(String.Format("通常クラス-Attribute.Inherited=False-GetCustomAttributes(False)=> {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(Test).GetCustomAttributes(GetType(InheritedAttribute), True).Any
Console.WriteLine(String.Format("継承クラス-Attribute.Inherited=True -GetCustomAttributes(True) => {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(Test).GetCustomAttributes(GetType(InheritedAttribute), False).Any
Console.WriteLine(String.Format("継承クラス-Attribute.Inherited=True -GetCustomAttributes(False)=> {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(Test).GetCustomAttributes(GetType(NotInheritedAttribute), True).Any
Console.WriteLine(String.Format("継承クラス-Attribute.Inherited=False-GetCustomAttributes(True) => {0}", p.Invoke))
log.Invoke(p)
p = Function() GetType(Test).GetCustomAttributes(GetType(NotInheritedAttribute), False).Any
Console.WriteLine(String.Format("継承クラス-Attribute.Inherited=False-GetCustomAttributes(False)=> {0}", p.Invoke))
log.Invoke(p)
Console.ReadLine()
End Sub
End Module
<AttributeUsage(AttributeTargets.Class, Allowmultiple:=False, Inherited:=True)>
Public Class InheritedAttribute
Inherits Attribute
End Class
<AttributeUsage(AttributeTargets.Class, Allowmultiple:=False, Inherited:=False)>
Public Class NotInheritedAttribute
Inherits Attribute
End Class
<InheritedAttribute(), NotInheritedAttribute()>
Public Class TestBase
Public Property Value As String
End Class
Public Class Test
Inherits TestBase
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment