473,396 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question about Attributes?

Is there a way to get the type (through reflection) that applied the
attribute from within the attribute?

For example:
using System;
using System.Reflection;
using DlgProc.MyAttributeNamespace;

namespace DlgProc.MyAttributeNamespace
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false,
Inherited=false)]
public class MyAttribute: System.Attribute
{
private string MyAttributeData;
private Type WhoCalledMe;

public MyAttribute(string Data)
{
MyAttributeData = Data;

// Would like to get the type (MyClass) that applied the
attribute, but how???
// WhoCalledMe = ??????
}

public string Data
{
get
{
return Data;
}
}
}
}

namespace DlgProc.MyClassNamespace
{
[MyAttribute("Stuff")]
public class MyClass
{
public MyClass()
{
}

public void DoSomethingStupid()
{
}
}
}
Nov 16 '05 #1
2 989
dlgproc,

No, it is not. You would have to supply the type. It's kind of like a
Type knowing what instances of it are out there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dlgproc" <dl*****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Is there a way to get the type (through reflection) that applied the
attribute from within the attribute?

For example:
using System;
using System.Reflection;
using DlgProc.MyAttributeNamespace;

namespace DlgProc.MyAttributeNamespace
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false,
Inherited=false)]
public class MyAttribute: System.Attribute
{
private string MyAttributeData;
private Type WhoCalledMe;

public MyAttribute(string Data)
{
MyAttributeData = Data;

// Would like to get the type (MyClass) that applied the
attribute, but how???
// WhoCalledMe = ??????
}

public string Data
{
get
{
return Data;
}
}
}
}

namespace DlgProc.MyClassNamespace
{
[MyAttribute("Stuff")]
public class MyClass
{
public MyClass()
{
}

public void DoSomethingStupid()
{
}
}
}

Nov 16 '05 #2
ok. thanks.

"Nicholas Paldino [.NET/C# MVP]" wrote:
dlgproc,

No, it is not. You would have to supply the type. It's kind of like a
Type knowing what instances of it are out there.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dlgproc" <dl*****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
Is there a way to get the type (through reflection) that applied the
attribute from within the attribute?

For example:
using System;
using System.Reflection;
using DlgProc.MyAttributeNamespace;

namespace DlgProc.MyAttributeNamespace
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false,
Inherited=false)]
public class MyAttribute: System.Attribute
{
private string MyAttributeData;
private Type WhoCalledMe;

public MyAttribute(string Data)
{
MyAttributeData = Data;

// Would like to get the type (MyClass) that applied the
attribute, but how???
// WhoCalledMe = ??????
}

public string Data
{
get
{
return Data;
}
}
}
}

namespace DlgProc.MyClassNamespace
{
[MyAttribute("Stuff")]
public class MyClass
{
public MyClass()
{
}

public void DoSomethingStupid()
{
}
}
}


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: SB | last post by:
I have an assignment where we are supposed to mimic a scheduler routine of an OS. We are supposed to schedule some processes, an each process has the following attributes... - process id -...
1
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. I have marked the section from the book that is of intertest by tagging it with BOOK START HERE...
6
by: Tim Mavers | last post by:
I have a series of database objects that represent things such as people, accounts, etc. I have a set of options (boolean) that I need to add to these objects. Normally I would just create a bit...
6
by: Developer | last post by:
Hello, I'm experimenting with and XmlTextReader sample, and don't understand how the attributes are being processed. Here's the input: <xsd:element name="AA" type="BB"...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
1
by: Marcel Hug | last post by:
Hello NG ! I have a tricky question about dataSet and dataTable. In my app I have 2 databases. In the first (called A1) database are the whole datas but I'm interesting in one table tbl1 (with 20...
11
by: Doru Roman | last post by:
Hello, I got a test and some of the questions were: A collection a files, which is the basic unit of .NET programming, is called: 1. package 2. assembly 3. library 4. copybook
7
by: Ryan | last post by:
I'm in the process of learning more about building my ASP.NET website to use my SQL datastore and am a bit confused about how ADO.NET works with ASP.NET. This Microsoft article implies that using...
28
by: Alan Isaac | last post by:
I have a class whose instances should only receive attribute assignments for attributes that were created at inititialization. If slots are not appropriate, what is the Pythonic design for this? ...
4
by: RobG | last post by:
I have always accessed attributes such as disabled using the DOM element property, however I was wondering about implementing a more generic function to get the values of attributes - which of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.