473,659 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to find all classes in an assembly having a customAttribute ?

Sky
Hello:

I understand that I can query an class def to find if it has a specific
custom attribute -- but if i wanted to find all classes in an assembly that
have that attribute, is there a way?

How about methods? Without recursing through every class/every method --
which could get very time consuming?

(PS: To give context to the above question: I am thinking of trying to
find/list all WebMethods in an assembly...sinc e I am trying to make an
IhttpHandler that would correctly direct incoming requests to the right
method...)

And while on the subject -- is there a way to find all classes that
implement a specific interface? Ie, can I find all classdefs in an assembly
that inherit from IMyInterface?

Thanks,
Sky
Nov 16 '05 #1
5 10109
Hi Sky,

"Sky" <fo****@xact-solutions.remov ethis.com> wrote in message
news:e$******** ******@tk2msftn gp13.phx.gbl...
Hello:

I understand that I can query an class def to find if it has a specific
custom attribute -- but if i wanted to find all classes in an assembly
that
have that attribute, is there a way?
using System.Reflecti on;

....

Assembly asm = Assembly.LoadFi le("MyAssembly. dll");
if (asm != null)
{
foreach(Type t in asm.GetTypes())
{
object[] attributes = t.GetCustomAttr ibutes();
foreach(attr in attributes)
{
if (attr is MyCustomAttribu te)
{
....
}
}
}
}
How about methods? Without recursing through every class/every method --
which could get very time consuming?
for methods:

MethodInfo[] methods = t.GetMethods();

Reflection is considered a relatively heavy operation.

And while on the subject -- is there a way to find all classes that
implement a specific interface? Ie, can I find all classdefs in an
assembly
that inherit from IMyInterface?


Yes, and this (IMHO) the preferred way to implement a plugin architecture.

Remember that if you load assemblies into your current running AppDomain,
you have no way to unload them - so it's wise to create a temporary
AppDomain,
load the assemblies that (possibly) contain plugins into it, check them, and
only
load the ones implementing your plugin interface (and the ones you choose)
into
the running AppDomain later.

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway
Nov 16 '05 #2
Hi,
"Sky" <fo****@xact-solutions.remov ethis.com> wrote in message
news:e$******** ******@tk2msftn gp13.phx.gbl...
Hello:

I understand that I can query an class def to find if it has a specific
custom attribute -- but if i wanted to find all classes in an assembly that have that attribute, is there a way?
Assembly.GetTyp es() should be the start point. From there you will have to
check if the type is a class , using Type.IsClass
How about methods? Without recursing through every class/every method --
which could get very time consuming?


You will have to check only the part of the class where you can apply the
attribute.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #3
Sorry, I did not see this before

You can iterate in all the classes from the assembly and then you can use
some method from the Type class to see if it implement that interface, you
can use GetInterface( string ) .

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


And while on the subject -- is there a way to find all classes that
implement a specific interface? Ie, can I find all classdefs in an assembly that inherit from IMyInterface?

Thanks,
Sky

Nov 16 '05 #4
Sky
Dear Lars:
Thanks very very much:

a) I had totally forgotten that GetTypes() would negate the need to
recurse -- so the process is quite painless!
b) thanks for the reminder on using a different AppDomain.
c) Yes -- the looking for IMyInterface was for exactly that pupose --
looking for plug-in elements :-)

d) But another issue came up last night that this all might might be useful
for -- searching through an assembly for WebService /WebMethod attributes --
it appears that if I want to make a IHttpHandler for webservices, it's going
to all be done by hand, parsing the incoming xml soap message, getting the
SoapAction, getting the arguments deserialized to objects (?part not figured
out) and then instantiating the asked for class..)
Very best,
Sky


"Lars Wilhelmsen" <la*****@NOSPAM .ifi.uio.no> wrote in message
news:eB******** ******@TK2MSFTN GP12.phx.gbl...
Hi Sky,

"Sky" <fo****@xact-solutions.remov ethis.com> wrote in message
news:e$******** ******@tk2msftn gp13.phx.gbl...
Hello:

I understand that I can query an class def to find if it has a specific
custom attribute -- but if i wanted to find all classes in an assembly
that
have that attribute, is there a way?
using System.Reflecti on;

...

Assembly asm = Assembly.LoadFi le("MyAssembly. dll");
if (asm != null)
{
foreach(Type t in asm.GetTypes())
{
object[] attributes = t.GetCustomAttr ibutes();
foreach(attr in attributes)
{
if (attr is MyCustomAttribu te)
{
....
}
}
}
}
How about methods? Without recursing through every class/every method --
which could get very time consuming?


for methods:

MethodInfo[] methods = t.GetMethods();

Reflection is considered a relatively heavy operation.

And while on the subject -- is there a way to find all classes that
implement a specific interface? Ie, can I find all classdefs in an
assembly
that inherit from IMyInterface?


Yes, and this (IMHO) the preferred way to implement a plugin architecture.

Remember that if you load assemblies into your current running AppDomain,
you have no way to unload them - so it's wise to create a temporary
AppDomain,
load the assemblies that (possibly) contain plugins into it, check them,

and only
load the ones implementing your plugin interface (and the ones you choose)
into
the running AppDomain later.

--
Lars Wilhelmsen
http://www.sral.org/
Software Engineer
Teleplan A/S, Norway

Nov 16 '05 #5
Sky
Dear Ignacio:

Both you and Lars made the answer perfectly clear -- thanks!
GetType/GetInterface -- just what I needed because:

a) I had totally forgotten that GetTypes() would negate the need to
recurse -- so the process is quite painless!
b) Lars reminded me to use a different AppDomain when reflecting across
external assemblies looking for plug-ins....good point.
c) Another issue came up last night that this all might might be useful
for -- searching through an assembly for WebService /WebMethod attributes --
it appears that if I want to make a IHttpHandler for webservices, it's going
to have to be all be done by hand (since the ASP.NET framework always will
expect that asmx page to help with the routing), parsing the incoming xml
soap message, getting the SoapAction, getting the arguments deserialized to
objects (?part not figured out) and then instantiating the asked for
class..)

Very best,
Sky

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:us******** ******@TK2MSFTN GP11.phx.gbl...
Sorry, I did not see this before

You can iterate in all the classes from the assembly and then you can use
some method from the Type class to see if it implement that interface, you
can use GetInterface( string ) .

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


And while on the subject -- is there a way to find all classes that
implement a specific interface? Ie, can I find all classdefs in an

assembly
that inherit from IMyInterface?

Thanks,
Sky


Nov 16 '05 #6

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

Similar topics

5
1630
by: Lucas Sain | last post by:
Hi, I have a form (A) which has a collection as a property. All my forms inhert from this base form. I'm tring to put some code in form A where I can get the fieldInfo from the form that imnherits from A. This is the code: Type t = this.GetType(); FieldInfo parameterInfo = t.GetField(field,BindingFlags.Instance |
10
2429
by: ptass | last post by:
Hi In asp.net 2.0 an aspx files .cs file is a partial class and all works fine, however, I thought I’d be able to create another class file, call it a partial class and have that compile and load as a 3rd partial class. This would be handy so i can generate standard code into one of the partial classes, while having my custom code untouched
2
1449
by: Michael | last post by:
Hi Everyone, This one may be a simple one, but here it goes. I have two assemplies and I'm trying to declare some of the classes in the second one. For example: Assembly 1: 'Have already set the reference to the other Assembly Imports FormBuilder.FormBuilder Public Class Class1 'Trying to Implement one of the Interfaces in the other Assembly 'Implements FormBuilder.FormBuilder.IFormProcessing
1
1463
by: papalarge | last post by:
Hey... so I have a server and a client app that are both using the same .dll for communication. I've built the library, gacutil'ed it to register it... I'm up to version 1.2.6 right now. When I run the client, I get an error during one of it's methods that says: "Cannot find the assembly SMailMan, Version=1.2.2.0, Culture=neutral, PublicKeyToken=....." I've removed all references I have to this, uninstalled old
1
1097
by: tcc.se7en | last post by:
I was having a bit of an issue with assembly references, but now it seems to have spiraled even further out of control. Not sure what I did to break it, any help is appreciated. Basically, I have this dll, which was a Web Application Project with some classes in it. I was trying to find a way to make it be globally accessible to several apps. Amongst the many things I tried, I installed it in the GAC. I still didn't have quite the...
0
1126
by: =?Utf-8?B?a2FybF9h?= | last post by:
Hello, I’m working on a company that is running a web site developed in .NET 1.1. We has started to use a third part software that is an signed assembly, cause to problem with mixing signed and unsigned assemblies in the same bin-folder we now has also started to signing our own assemblies. We do a release of new code a couple of times per week and after we started with signed assemblies the following exception has started to occur...
13
1888
by: Carl Johansson | last post by:
Being quite new to C#, I may have misunderstood this. If so please bear with me! As far as I can understand, any instances of a class that implements the IDisposable interface must call the Dispose method not create leaks of resources!? This can be accomplished by explicitly calling Dispose or through the "using" statement. For example, a recursive method that creates hundreds or thousands of instances of, for example,...
4
2122
karthickkuchanur
by: karthickkuchanur | last post by:
hai members, could i able to see the what r the the interface and classes in java using like javap
2
2729
by: jehugaleahsa | last post by:
Hello: We recently moved from Visual Studio 2005 to 2008. Today I published an older web application using 2008. I had to migrate this project. When I run this project from my local machine, it works fine. However, when I publish it using FrontPage Server Extensions to our development server, the web page always fails with the following message: Could not load type 'CorrespondenceLogDataTable' from assembly
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7360
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1982
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.