Re: Instance attribute values

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Rob Teixeira [MVP] (RobTeixeira_at_@msn.com)
Date: 09/22/04


Date: Tue, 21 Sep 2004 20:34:20 -0400

Nope. The attributes are affecting Type metadata. This is per Type, not per
instance. Once you compile, that attribute's value is serialized into the
metadata of the assembly. You can create new attributes at runtime with
different values, but the attribute values tied to your Types in the
metadata can't be changed. In other words, if you query using Reflection,
you'll always get the compiled attribute values for a Type or Type member.
Anything in the way of instance data that is manipulated at runtime should
be an instance field.

-Rob Teixeira [MVP]

"ljlevend" <ljlevend@nospam.nospam> wrote in message
news:05152471-5E59-4F05-A26A-1138ABA6F47F@microsoft.com...
> Is there any way to have an attribute with a value that is unique to an
> instance of an object? For example, let say I have the following:
>
> Public Class MyAttribute
> Inherits System.Attribute
> Public Value As Boolean
> Public Sub New(ByVal value As Boolean)
> Me.Value = value
> End Sub
> End Class
>
> Public Class MyClassWithAttribute
> <MyAttribute(True)> _
> Public Property SomeProperty As Object
> ...
> End Property
> End Class
>
> Public Class MyWorkingClass
> Public myClassWithAttribute1 As New MyClassWithAttribute
> Public myClassWithAttribute2 As New MyClassWithAttribute
> ...
> End Class
>
> Now, what I want to do is to be able to set MyAttribute.Value of
> myClassWithAttribute1.SomeProperty to False, but to keep MyAttribute.Value
of
> myClassWithAttribute2.SomeProperty set to True. Is this possible?
>
> Thanks,
> Lance
>



Relevant Pages

  • Re: Why use the new modifier?
    ... I have a mature framework where much is held in metadata, ... All classes actually have a constructor with 2 ... > rules given these statuses. ... > public class WorkOrderStatus:OrderStatus ...
    (microsoft.public.dotnet.languages.csharp)
  • Instance attribute values
    ... Public Sub New ... Public Class MyClassWithAttribute ... Public Property SomeProperty As Object ... Public myClassWithAttribute1 As New MyClassWithAttribute ...
    (microsoft.public.dotnet.languages.vb)