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

Dynamically Retrieve Method Names

Frinavale
9,735 Expert Mod 8TB
Is there a way to dynamically retrieve the names of the public methods for an Object?
Dec 5 '08 #1
7 1668
Plater
7,872 Expert 4TB
RRRRRRRRReflection Power!
You can do it with Reflection

(gathering info, please wait...)
Its C#, but it's two lines
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Get the Type for your object
  3. Type t = myObject.GetType();
  4. //Invoke the .GetMembers() (or maybe .GetMethods)
  5. MemberInfo[] mems = t.GetMember("SomeNameOfMember", BindingFlags.Public );
  6. //I have a public function called Start(), so I put in "Start" instead of "SomeNameOfMember"
  7.  
  8.  
Dec 5 '08 #2
Frinavale
9,735 Expert Mod 8TB
Ok, I didn't see the Type.GetMethods() method because it wasn't being listed by the intellisense (doh).

Anyways, I keep getting back an array with the length of 0.
Expand|Select|Wrap|Line Numbers
  1. Dim t As Type = GetType(FooBar)
  2. Dim info() As MemberInfo = t.GetMethods 'Nothing's returned by this call
  3. Dim str As New StringBuilder
  4. For Each member As MemberInfo In info
  5.      str.Append(member.Name)
  6.      str.Append(vbLf)
  7. Next
  8. MessageBox.Show(str.ToString)
Dec 5 '08 #3
Plater
7,872 Expert 4TB
Did you try this:
MemberInfo[] mems=t.GetMembers(BindingFlags.Public);

or I guess for you:
Dim mems() as MemberInfo = t.GetMembers(BindingFlags.Public)

I never had any trouble with:
Expand|Select|Wrap|Line Numbers
  1. public static List<string> GetMemberNames(object ReflectedObject)
  2. {
  3.   List<string> retval = new List<string>();
  4.   if (ReflectedObject != null)
  5.   {
  6.       MemberInfo[] mems = ReflectedObject.GetType().GetMembers();
  7.       foreach (MemberInfo mi in mems)
  8.       {
  9.             string name = mi.Name;
  10.             retval.Add(name);
  11.       }
  12.   }
  13.   return retval;
  14. }
  15.  
Dec 5 '08 #4
Frinavale
9,735 Expert Mod 8TB
Getting the Methods for the String class works fine:
Expand|Select|Wrap|Line Numbers
  1.         Dim t As Type = GetType(String)
  2.         Dim info() As MemberInfo = t.GetMethods
  3.  
  4.         Dim str As New StringBuilder
  5.  
  6.         For Each member As MemberInfo In info
  7.             str.Append(member.Name)
  8.             str.Append(vbLf)
  9.         Next
  10.         MessageBox.Show(str.ToString)
It must have something to do with Interop.....hmmm
Dec 5 '08 #5
Plater
7,872 Expert 4TB
Well you never said anything about it being a COM object :-P
Interop are all Interface calls, you will have to change your binding flags
Edit: I think
I couldn't get the watch window to show members of a COM object (Say Excel.Application) but the intellisense worked
Dec 5 '08 #6
Frinavale
9,735 Expert Mod 8TB
@Plater
The GetFields() method worked for the COM type though...I didn't think that there would be a problem with the GetMethods() method but apparently it doesn't like it.

Mind you this doesn't surprise me because even though the GetFields() method works the SetValue() (for the field) does not work....well not without casting the type into a ValueType first and then using the SetValue.
Dec 5 '08 #7
Frinavale
9,735 Expert Mod 8TB
@Plater
I've tried all of the Bindings that deal with COM objects and still can't retrieve the methods.
Dec 5 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Falc2199 | last post by:
Hi, Does anyone know how to make this work? var sectionId = 5; repeat_section_sectionId(); function repeat_section_5(){ alert("firing"); }
4
by: cjm | last post by:
I have two problems that I suspect will be bread-and-butter problems for the more experienced guys on here, but I'm not the greatest with js. NB: Code snippets at the bottom The first problem...
8
by: Arpan | last post by:
A Form has a select list which lists all the column names of a SQL Server database table. Users will select one or more than one column from this select list & after submitting the Form, the...
9
by: Patrick.O.Ige | last post by:
I have a code below and its a PIE & BAR CHART. The values now are all static but I want to be able to pull the values from a database. Can you guys give me some ideas to do this? Thanks ...
4
by: hb | last post by:
Hi, When I add an asp:button (ex: id=btnLog) on home.aspx, I need to create btnLog_Click() event in home.aspx.cs, and also link this event and the button in OnInit() method by adding:...
2
by: wapsiii | last post by:
I have a form to which I add sets of input controls ie. textbox 1, dropdownlist 1, textbox 2, dropdownlist 1, textbox 3, dropdownlist 3. The number of sets vary. I give them unique IDs the...
4
by: Daniel Nogradi | last post by:
Is it possible to have method names of a class generated somehow dynamically? More precisely, at the time of writing a program that contains a class definition I don't know what the names of its...
5
by: mrtr33 | last post by:
I have a class table where I want to access the names of courses and the description of each course. The names of the courses go in a drop down list. Based on the user selection, the description...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
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: 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:
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
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
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
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...
0
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,...

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.