473,507 Members | 13,597 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reflection

hi,
I have an assembly with many classes in it.
and some of the methods in those classes have this attribute StoredProcedure
before them, which takes different values . (i basically use it to store
storedprocedure names that this function calls.)

[MyCompany1.Data.StoredProcedure("spGetCustomers")]
[MyCompany1.Data.StoredProcedure("spGetSales")]
public void GetCustomers()
{
....
}
[MyCompany1.Data.StoredProcedure("spGetTotalSales")]
public void GetTotalSales()
{
....
}

etc.
what i want to do is iterate all those methods that have this attribute
StoredProcedure
and find out the value (eg: spGetCustomers and spGetSales in the first
case.)

how do i do this.

I havent figured out how do iterate all the classes
and
how do i find out if a method uses that attribute.
//This simply lists all the methods.
Type type = (typeof(MyCompany1.Dal.DataAccessClass1));
MethodInfo[] methodInfo = type.GetMethods();
for(int i =0;i< methodInfo.Length; i++)
{
Console.WriteLine("Name = " + methodInfo[i].ToString());

}

am i on the right track?
thnx
Nov 16 '05 #1
3 2449
Check out the Assembly class You can use the Static Load,
GetExcecutingAssembly, GetCallingAssembly/etc methods to get a reference to
an assembly object, then you can use the GetTypes method to return Type
objects representing all of the classes in the assembly.

The MethodInfo objects (that your code below is already getting) have a
method called GetCustomAttributes that should return your attributes.
"frazer" <ic***@hotmail.com> wrote in message
news:O2**************@TK2MSFTNGP10.phx.gbl...
hi,
I have an assembly with many classes in it.
and some of the methods in those classes have this attribute StoredProcedure before them, which takes different values . (i basically use it to store
storedprocedure names that this function calls.)

[MyCompany1.Data.StoredProcedure("spGetCustomers")]
[MyCompany1.Data.StoredProcedure("spGetSales")]
public void GetCustomers()
{
...
}
[MyCompany1.Data.StoredProcedure("spGetTotalSales")]
public void GetTotalSales()
{
...
}

etc.
what i want to do is iterate all those methods that have this attribute
StoredProcedure
and find out the value (eg: spGetCustomers and spGetSales in the first
case.)

how do i do this.

I havent figured out how do iterate all the classes
and
how do i find out if a method uses that attribute.
//This simply lists all the methods.
Type type = (typeof(MyCompany1.Dal.DataAccessClass1));
MethodInfo[] methodInfo = type.GetMethods();
for(int i =0;i< methodInfo.Length; i++)
{
Console.WriteLine("Name = " + methodInfo[i].ToString());

}

am i on the right track?
thnx

Nov 16 '05 #2
hi,
thanx that works fine.
just one little problem.
i have a class library project that has many classes in it.
how do i iterate the class library to see all the classes it has?
the class library is in the namespace "MyCompany1.Data"

currently i am just testing teh application for 1 class.
Assembly assembly = Assembly.GetAssembly(typeof(MyCompany1.Data.Class1 ));
but i need access to class2 and all others.

thnx
Nov 16 '05 #3
n!
> currently i am just testing teh application for 1 class.
Assembly assembly = Assembly.GetAssembly(typeof(MyCompany1.Data.Class1 ));
but i need access to class2 and all others.


You can obtain the list of types in an assembly with the GetTypes() method.
eg:

public void ScanTypes()
{
Assembly assembly = typeof( MyCompany1.Data.Class1 ).Assembly;

foreach ( Type type in assembly.GetTypes() )
{
Console.WriteLine( "Scanning type " + type.FullName + "." );

// Do something with 'type'...
}
}

n!
Nov 16 '05 #4

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

Similar topics

0
1360
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
7341
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
1962
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
0
1535
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
3
2319
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
9
3829
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
17
2291
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
1684
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7314
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,...
0
7372
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
5623
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,...
0
4702
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...
0
3191
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
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 ...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.