How to pass a method as a parameter


Author
Message
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
I want to set up a General Purpose type method which will have two parameters, a Business Object, and a Method Name of the business object. The method then needs to just run the method.

e.g.

private void RunABoMethod(BusinessObject myBo, Methodname MyMethodname))
{
      myBO.MyMethodname;
}
so some examples of how this might be called:
RunABoMethod(CustomerBO,"GetAllrecords");
or
RunABomethod(StockBO,"GetTop20");


Any suggestions / coding examples on how this might be accomplished (c#) would be appreciated. A few searches indicate that Func (Dot Net 3.5) or delgates could be used but I have no experience of using these constructs
Replies
Edhy Rijo
E
StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)StrataFrame VIP (6.4K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Gerard,

I see what you meant now.  I have not done that, but my guess is that you would have to use the Reflection class or something like lazy binding,  to sort of been able to execute the method in the passed BO.  Also you must CAST your bo to the proper type to be able to find your method name, this could get ugly.

Probably looking at your logic from a different side you can achieve this easier.  Using your sample, you will always know at some point which BO and Method you want to run, so unless this must be totally generic and data driven, you can simply have an Enumeration listing the pre-existing methods and just use the old Select Case MyEnumeration and just pre-code your BO method calling. 
Again, this may not be generic enough for your needs, but could be faster, easier and safe. Doze

Edhy Rijo

Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)Advanced StrataFrame User (866 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
Hi Edhy.

Yes, I was trying to make this fairly Generic, though I see your  point about using an enumeration as a possible way to go.

I think this can be done using Delegates and/or functions , just have no experience of using them... perhaps someone familiar with these might jump in.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search