473,473 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Questions about refelection

Hello,

I'm programming a program, where I don't know which objects are needed. So I
want to use the mechanism of reflection.

I can create the objects I need by reflection, but there are some questions
I don't have a solution. Perhaps anybody can help?

First i want to write a little code down here, the code isn't so important
but it describes the structure of my classes.
Expand|Select|Wrap|Line Numbers
  1. public interface ITest
  2. {
  3. void Find(string keyword);
  4. }
  5.  
  6. public class CTest : ITest
  7. {
  8. string index;
  9. void Find(string text)
  10. {}
  11. public CTest()
  12. {}
  13. }
  14.  
  15. public class CSolute
  16. {
  17. protected ITest testclass;
  18. protected string text;
  19. public ITest Index
  20. {
  21. get{return testclass;}
  22. }
  23. public string Text
  24. {
  25. get{return text;}
  26. set{text=value;}
  27. }
  28. public CSolute()
  29. {}
  30. }
  31.  
The class CSolute is saved in an arraylist and all fields are filled (the
fields of the class CTest are filled, too)
I've managed to get an object of the class CSolute

And now my problems:
1. I want to display the classname of CSolute.testclass (not the name of the
interface!). How can I manage this?
2. How can I get the object of the class CSolute.testclass by reflection?
3. How can I invoke the method CTest.Find by reflection?

I would be glad if anyone of you have an answer.

cu Thomas

Nov 16 '05 #1
2 1237
Thomas,
public class CTest : ITest
{
string index;
void Find(string text)
{}
Find must be public for your code to compile.

2. How can I get the object of the class CSolute.testclass by reflection?
The easiest way would be to use the Index property.

object test =
soluteObj.GetType().GetProperty("Index").GetValue( soluteObj, null);

1. I want to display the classname of CSolute.testclass (not the name of the
interface!). How can I manage this?
test.GetType().Name

3. How can I invoke the method CTest.Find by reflection?


test.GetType().GetMethod("Find").Invoke(test, new object[] {"abc"});

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi Thomas,
And now my problems:
1. I want to display the classname of CSolute.testclass (not the name of the interface!). How can I manage this?
object o = csolute_Instance.Index.GetType().FullName
this will give you the name including the namespace of the type returned.
2. How can I get the object of the class CSolute.testclass by reflection? What yo mean with this ? you get the object just calling the property, it's
only that you see the "ITest" part of it. you can always cast it to the type
if you know what type it is.

3. How can I invoke the method CTest.Find by reflection?

Why using reflection for this? just use the interface as declared
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #3

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

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
8
by: Krypto | last post by:
Hi, I have used Python for a couple of projects last year and I found it extremely useful. I could write two middle size projects in 2-3 months (part time). Right now I am a bit rusty and trying...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
1
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: ramu | last post by:
C# Interview Questions and Answers8 http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html C# Interview Questions and Answers7...
0
by: reema | last post by:
EJB Interview Questions http://interviewdoor.com/technical/EJB-Interview-Questions.htm CSS Interview Questions http://interviewdoor.com/technical/CSS-Interview-Questions.htm C Interview Questions...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.