Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CustomAttributeTypedArgument.Value Property

Gets the value of the typed argument.

Namespace: System.Reflection
Assembly: mscorlib (in mscorlib.dll)

public Object Value { get; }
/** @property */
public Object get_Value ()

public function get Value () : Object

Not applicable.

Property Value

An object that represents the value of the typed argument, or a generic ReadOnlyCollection of type CustomAttributeTypedArgument that represents the values of an array-type argument.

The ArgumentType property supplies the actual type of the value returned by the Value property, or the array type if the argument is an array.

The following example defines a custom attribute with four constructors and four properties. Two of the properties are read-only, and are set by using the positional parameters of the constructors. The other two properties are read/write, and can be set only by using named arguments. One positional property is an array of strings, and one named property is an array of integers.

The attribute is applied to the assembly, to a type declared in the assembly, to a method of the type, and to a parameter of the method. Different constructors are used for these cases. When executed, the assembly loads itself into the reflection-only context and displays the custom attributes.

The attribute that is applied to the type demonstrates array properties, with both positional and named arguments.

The Value property is used in the ShowValueOrArray method that displays custom attribute data, to display the values of attributes.

using System;
using System.Reflection;
using System.Collections.Generic;

// The example attribute is applied to the assembly.
[assembly:Example(ExampleKind.ThirdKind, Note="This is a note on the assembly.")]

// An enumeration used by the ExampleAttribute class.
public enum ExampleKind
{
    FirstKind, 
    SecondKind, 
    ThirdKind, 
    FourthKind
};

// An example attribute. The attribute can be applied to all
// targets, from assemblies to parameters.
//
[AttributeUsage(AttributeTargets.All)]
public class ExampleAttribute : Attribute
{
    // Data for properties.
    private ExampleKind kindValue;
    private string noteValue;

    // Constructors. The parameterless constructor (.ctor) calls
    // the constructor that specifies ExampleKind, and supplies the
    // default value.
    //
    public ExampleAttribute(ExampleKind initKind)
    {
        kindValue = initKind;
    }
    public ExampleAttribute() : this(ExampleKind.FirstKind) {}

    // Properties. The Note property must be read/write, so that it
    // can be used as a named parameter.
    //
    public ExampleKind Kind { get { return kindValue; }}
    public string Note    
    {
        get { return noteValue; }
        set { noteValue = value; }
    }
}

// The example attribute is applied to the test class.
//
[Example(ExampleKind.SecondKind, Note="This is a note on the class.")]
public class Test
{
    // The example attribute is applied to a method, using the
    // parameterless constructor and supplying a named argument.
    // The attribute is also applied to the method parameter.
    //
    [Example(Note="This is a note on a method.")]
    public void TestMethod([Example] object arg) { }

    // Main() gets objects representing the assembly, the test
    // type, the test method, and the method parameter. Custom
    // attribute data is displayed for each of these.
    //
    public static void Main()
    {
        Assembly asm = Assembly.ReflectionOnlyLoad("Source");
        Type t = asm.GetType("Test");
        MethodInfo m = t.GetMethod("TestMethod");
        ParameterInfo[] p = m.GetParameters();

        Console.WriteLine("\r\nAttributes for assembly: {0}", asm);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(asm));
        Console.WriteLine("\r\nAttributes for type: {0}", t);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(t));
        Console.WriteLine("\r\nAttributes for member: {0}", m);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(m));
        Console.WriteLine("\r\nAttributes for parameter: {0}", p);
        ShowAttributeData(CustomAttributeData.GetCustomAttributes(p[0]));
    }

    private static void ShowAttributeData(
        IList<CustomAttributeData> attributes)
    {
        foreach( CustomAttributeData cad in attributes )
        {
            Console.WriteLine("   {0}", cad);
            Console.WriteLine("      Constructor: {0}", cad.Constructor);

            Console.WriteLine("      Constructor arguments:");
            foreach( CustomAttributeTypedArgument cata 
                in cad.ConstructorArguments )
            {
                Console.WriteLine("         Type: {0} Value: {1}", 
                    cata.ArgumentType, cata.Value);
            }

            Console.WriteLine("      Named arguments:");
            foreach( CustomAttributeNamedArgument cana 
                in cad.NamedArguments )
            {
                CustomAttributeTypedArgument cata = cana.TypedValue;
                Console.WriteLine("         MemberInfo: {0}", 
                    cana.MemberInfo);
                Console.WriteLine("         Type: {0} Value: {1}", 
                    cata.ArgumentType, cata.Value);
            }
        }
    }
}

/* This code example produces output similar to the following:

Attributes for assembly: source, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
   [ExampleAttribute((ExampleKind)2, Note = "This is a note on the assembly.")]
      Constructor: Void .ctor(ExampleKind)
      Constructor arguments:
         Type: ExampleKind Value: 2
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on the assembly.
   [System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int32)8)]
      Constructor: Void .ctor(Int32)
      Constructor arguments:
         Type: System.Int32 Value: 8
      Named arguments:

Attributes for type: Test
   [ExampleAttribute((ExampleKind)1, Note = "This is a note on the class.")]
      Constructor: Void .ctor(ExampleKind)
      Constructor arguments:
         Type: ExampleKind Value: 1
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on the class.

Attributes for member: Void TestMethod(System.Object)
   [ExampleAttribute(Note = "This is a note on a method.")]
      Constructor: Void .ctor()
      Constructor arguments:
      Named arguments:
         MemberInfo: System.String Note
         Type: System.String Value: This is a note on a method.

Attributes for parameter: System.Object arg
   [ExampleAttribute()]
      Constructor: Void .ctor()
      Constructor arguments:
      Named arguments:
*/

ArgumentType | ReflectionOnlyLoad

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.