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

creating delegate instance from method retrieved through reflection

Hi All,

I am trying to use the attribute/reflection system to as much
potential as I can think of, but I've run into a snag. I would
appreciate it if someone would point me in the right direction.

First my question: How am I supposed to make instances of delegates
when I have the method (that I want to make the delegate of) retrieved
through reflection?

My situation is like this:

* I have one delegate declaration:
delegate void TopLevelDelegate( );

* I have two Attributes:
class TopLevelAttribute : Attribute
{
private string mDescription;
public TopLevelAttribute( string desc )
{
mDescription = desc;
}

public string Description { get { return mDescription; } }
}

class ContainsTopLevelAttribute : Attribute
{
}

* An example of how I would like to use it is as follows:
[ContainsTopLevel()]
class Foo
{
[TopLevel("Bar")]
static void SomeMethod()
{
}
}

* somewhere I have a method that looks like this:
bool AddDelegateInstance( string description, TopLevelDelegate tld );

* And finally here is where I have a problem. I am trying to iterate
through all of this assembly's classes and find types that are
annotated with ContainsTopLevelAttribute, Then for those classes I
want to look for all methods which are annotated with
TopLevelAttribute. This is what I tried:
private static void findAllTopLevels()
{
Assembly thisAssembly = Assembly.GetExecutingAssembly();
Type[] types = thisAssembly.GetTypes();
foreach ( Type classType in types )
foreach ( Attribute classAtr in classType.GetCustomAttributes(
typeof(ContainsTopLevelAttribute), false ) )
{
ContainsTopLevelAttribute ctla = classAtr as
ContainsTopLevelAttribute;
if ( ctla != null )
foreach ( MethodInfo mi in classType.GetMethods() )
foreach( Attribute methodAtr in mi.GetCustomAttributes(
typeof( TopLevelAttribute ), false ) )
{
TopLevelAttribute tla = methodAtr as TopLevelAttribute;
if ( tla != null )
{
// ERROR RIGHT BELOW HERE
AddDelegateInstance( tla.Description, new
TopLevelDelegate( mi.Name ) );
}
}
}
}

* I get the following error:
error CS0118: 'System.Reflection.MemberInfo.Name' denotes a 'property'
where a 'method' was expected

* I will repeat my question: How am I supposed to make instances of
delegates when I have the method retrieved through reflection?

Friendly Regards
Pieter Breed
Nov 16 '05 #1
1 2349
Pieter Breed <pi**********@gmail.com> wrote:

<snip>
* I will repeat my question: How am I supposed to make instances of
delegates when I have the method retrieved through reflection?


Use Delegate.CreateDelegate(Type, MethodInfo)

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

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

Similar topics

3
by: Clinton Pierce | last post by:
I can create a delegate like this, and everything works fine: class Foo { private delegate void NextPanel(); private NextPanel myself; // And later in a method private void EffStart() {
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
8
by: Ympostor | last post by:
Why can't I do this (it gives a syntactic error):? foreach (System.Reflection.PropertyInfo oProp in oObject.GetType().GetProperties()) { if ((oProp.PropertyType.IsGenericType) &&...
22
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have an Assembly that has various usercontrols that all implemnet an Interface The Interface public interface ISettings { bool SaveSettings();
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.