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

Does C# know respondsto: or similar?

Hello!

I would like to send a method based on a string to an object.

In Smalltalk, Ruby and Objective-C exists a respondsto: method which allows you to ask an object dynamically if it responds to a certain method and also to invoke the method with some parameters.

Is there such a functionality in C#? I get a string with a methodname and would like to check if an object has the method "amethod:". If so then I would like to send this message to the object and pass some parameters along.

Does this work in C#?

Can I get a runtime-list of methods supported from an object and iterate myself through the list and compare with the string and invoke the method then?

Thank you for any help,
regards
Philipp Ott
Nov 15 '05 #1
1 1490
Philipp Ott <ph*********@hotmail.com> wrote:
I would like to send a method based on a string to an object.

In Smalltalk, Ruby and Objective-C exists a respondsto: method
which allows you to ask an object dynamically if it responds
to a certain method and also to invoke the method with some
parameters.

Is there such a functionality in C#? I get a string with a methodname
and would like to check if an object has the method "amethod:". If
so then I would like to send this message to the object and pass
some parameters along.


Firstly - terminology-wise, people don't tend to talk about "sending
messages" in .NET (or at least, not in this context) - it's more just
invoking or calling methods. Anyway, moving on - yes, you absolutely
can do this, although it's not part of C# itself, it's part of the .NET
framework, and it's called reflection.

First you need to get the Type object related to your instance, which
you do using the GetType() method. You can then call GetMethod() on the
Type, and then calling Invoke() on the MethodInfo returned by
GetMethod. If you look up all of those methods in MSDN you'll get far
more information than I could possibly go into in a post :)

Hope that helps - let me know if you have any problems.

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

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

Similar topics

4
by: Robin Tucker | last post by:
Hi, I'm trying to determine with my program whether or not a given database supports a given feature set. To do this I'm querying for certain stored procedures in the sysobjects table and if...
5
by: John Creighton | last post by:
What does BCPL stand for is it one language or a family of languages. My bust guess it the B C Programming Languages. But that is just a short in the dark. I am also not sure what languages fall...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
4
by: noname | last post by:
I'm learning C# generics recenly. How do i do to write the similar Java function below in C#? void printCollection(Collection<? extends A> c) { for (Object e: c) { System.out.println(e); }} ...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does K = parseInt('09') set K to 0? ----------------------------------------------------------------------- ...
19
by: active | last post by:
The ColorPalette class has no constructor so how does one use it? I define a variable by: Dim cp as ColorPalette but don't know how assign an object to the variable. Thanks in advance
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
11
by: tracy | last post by:
Hi, I really need help. I run this script and error message appeal as below: drop trigger log_errors_trig; drop trigger log_errors_trig ERROR at line 1: ORA04080: trigger 'LOG_ERRORS-TRIG'...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.