473,382 Members | 1,692 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,382 software developers and data experts.

Searching for attributes

I want to implement custom attributes. They can be used in several
assemblies. I want these attributes to register somehow. So I need the
attributes constructor to be called when an attributes is assigned to a
class (in several assemblies).

I found out, that the construtor is only called, when I "search" for
custom attributes in an assembly. Can this be done another way? If not,
how can I "search" in all assemblies?

--
Sleepless in Berlin
Wernfried
Nov 16 '05 #1
3 1622
If I understand you correctly, you can write a static Register method for
your attribute and call it the first thing your first assembly is run, and
write a static constructor for your attribute. Here is an example:

public class MyCustomAttribute : Attribute
{
static MyCustomAttribute()
{
Console.WriteLine("MyCustomAttribute constructor called");
}

public static vois Register()
{
// Do something to register the attribute
}
}

[MyCustom]
public class A
{
public A()
{
}

public void Search()
{
Console.WriteLine("Search started...");
// Write your search code here
}

public static void Main()
{
// This can be called in the init method of an assembly
MyCustomAttribute.Register();
A a = new A();
a.Search();
}
}

Angie

"Wernfried Schwenkner" <we*************************@sesa.de> wrote in
message news:oq***********@news.bln.sesa.de...
I want to implement custom attributes. They can be used in several
assemblies. I want these attributes to register somehow. So I need the
attributes constructor to be called when an attributes is assigned to a
class (in several assemblies).

I found out, that the construtor is only called, when I "search" for
custom attributes in an assembly. Can this be done another way? If not,
how can I "search" in all assemblies?

--
Sleepless in Berlin
Wernfried

Nov 16 '05 #2
In article <zs********************@comcast.com>, ay*****@yahoo.com
says...
If I understand you correctly, you can write a static Register method for
your attribute and call it the first thing your first assembly is run, and
write a static constructor for your attribute. Here is an example:


As far as I know, a static constructor is called only once. And I want
to register the class the attributed is assigned on, not the attribute.

It is no problem to search an assembly for all occurences of my custom
attribute. But I don't know how to detect all the assemblies of my
application.
--
Sleepless in Berlin
Wernfried
Nov 16 '05 #3
Wernfried Schwenkner <we*************************@sesa.de> wrote:
It is no problem to search an assembly for all occurences of my custom
attribute. But I don't know how to detect all the assemblies of my
application.


You can find all the assemblies loaded within any particular AppDomain
using AppDomain.GetAssemblies. If you can get in early enough, you
could also just subscribe to the AppDomain.AssemblyLoad event, and fix
things up every time an assembly is loaded.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
3
by: Michael Hertz | last post by:
I have hundreds of samples of XML documents on my harddisc. But all of them lack the one or another feature of XML. Some XML documents have no attributes some others are rather flat (nesting...
7
by: Info 3000 | last post by:
Hello, I'm beginner in XML. I have just a little question : I understand that I can write : <Book> <Title> A nice day </Title> <Author> James Nicepen </Author> </Book>
2
by: Jack | last post by:
Hi, I have come to point in my db design where I'm trying to figure which is the best approach in making it generic (does this really matter?!?) Senario - I have a table called JOBS and this...
3
by: googleboy | last post by:
Hi there. I have defined a class called Item with several (about 30 I think) different attributes (is that the right word in this context?). An abbreviated example of the code for this is: ...
0
by: Wernfried Schwenkner | last post by:
I want to implement custom attributes. They can be used in several assemblies. I want these attributes to register somehow. So I need the attributes constructor to be called when an attributes is...
0
by: vissuyk | last post by:
Hi, I am new to xsd, any help is greatly appreciated. I will have customers define xsd. In there they will designate an attribute to be of certian type (e.g some:transid). When I see this in...
2
by: prabhupr | last post by:
Hi Folks I was reading this article (http://www.dotnetbips.com/articles/displayarticle.aspx?id=32) on "Custom Attribute", written by Bipin. The only thing I did not understand in this article...
0
by: richardaz | last post by:
I have been working on a project to search for 250,000 different records in ActiveDirectory. The process takes a long time and I have tried everything to make it faster. Currently using...
2
by: cloftis | last post by:
Using VS2003, VB and MSHTML, Using an HTMLSpanElement I want to enumerate the attributes of a SPAN tag. 1 'For testing sake 2 Dim strMarkup as String = "<span attr1='somevalue'...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.