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

Reflection ?

Hi
I have let's say 10 classes and I need a function that will call a method on
them. But this method name is the same in each of this class. For this
method class can be initiated by constructor without params. Is there a
simple way to invoke Delete() function in a given class just by giving it
name ? Other than using reflection ?
Function will look like this:
void deleteSomeThing(string className)
{
// some code that will do : className.Delete();
}
Jarod

Mar 25 '06 #1
8 1322
Hi Jarod
I think it is better to use polymorphism ( by interface or by
inheritance) to invoking a method with different implementation.

I hope this helps
A.Hadi

Mar 25 '06 #2
Are you looking to call the Delete method against an object or the class
itself? In other words, is the Delete method static?

--
Tim Wilson
..NET Compact Framework MVP

"Jarod" <bl*****@NOSPAM.gazeta.pl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi
I have let's say 10 classes and I need a function that will call a method on them. But this method name is the same in each of this class. For this
method class can be initiated by constructor without params. Is there a
simple way to invoke Delete() function in a given class just by giving it
name ? Other than using reflection ?
Function will look like this:
void deleteSomeThing(string className)
{
// some code that will do : className.Delete();
}
Jarod

Mar 25 '06 #3
vj
I don't think so, to my knowledge.. Would be interested in knowing if there
is any available

VJ

"Jarod" <bl*****@NOSPAM.gazeta.pl> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi
I have let's say 10 classes and I need a function that will call a method
on them. But this method name is the same in each of this class. For this
method class can be initiated by constructor without params. Is there a
simple way to invoke Delete() function in a given class just by giving it
name ? Other than using reflection ?
Function will look like this:
void deleteSomeThing(string className)
{
// some code that will do : className.Delete();
}
Jarod

Mar 25 '06 #4
> Are you looking to call the Delete method against an object or the class
itself? In other words, is the Delete method static?

--


It can be if it makes diffrence ;) Or makes it easier ;)
Jarod
Mar 25 '06 #5
From the code that you posted (which may have been pseudo code) it makes it
look like it could be a static method. I wasn't sure. I was actually
thinking that if you're using objects then this would be a classic use of
polymorphism. Or, for some reason, do you only have the name of the class as
a string?
http://msdn2.microsoft.com/en-us/lib...52(VS.80).aspx

private void button1_Click(object sender, EventArgs e)
{
DerivedClassA a = new DerivedClassA();
DerivedClassB b = new DerivedClassB();
Delete(a);
Delete(b);
}

private void Delete(BaseClass c)
{
c.Delete();
}

....

public abstract class BaseClass : System.Object
{
public virtual void Delete()
{
}
}

public class DerivedClassA : BaseClass
{
public override void Delete()
{
MessageBox.Show(this.ToString());
}
}

public class DerivedClassB : BaseClass
{
public override void Delete()
{
MessageBox.Show(this.ToString());
}
}

--
Tim Wilson
..NET Compact Framework MVP

"Jarod" <bl*****@NOSPAM.gazeta.pl> wrote in message
news:uG*************@TK2MSFTNGP10.phx.gbl...
Are you looking to call the Delete method against an object or the class
itself? In other words, is the Delete method static?

--


It can be if it makes diffrence ;) Or makes it easier ;)
Jarod

Mar 25 '06 #6
> From the code that you posted (which may have been pseudo code) it makes
it
look like it could be a static method. I wasn't sure. I was actually
thinking that if you're using objects then this would be a classic use of
polymorphism. Or, for some reason, do you only have the name of the class
as
a string?


There is 10 really diffrent classes but they have one common thing ;) The
delete funcion ;) And I chose to invoke it by late binding ;) Just to learn
something new.
Jarod

Mar 25 '06 #7
If all the classes share a common custom base class then you can use the
polymorphic approach. You could also define an interface and have all the
classes implement that interface to force the implementation of a Delete
method. Either way what you'd be trying to do is guarantee that the object
that is passed to the method has a Delete method. Otherwise, you'd need to
use reflection to look up if the type of the object passed into the method
actually has a Delete method that can be invoked.

--
Tim Wilson
..NET Compact Framework MVP

"Jarod" <bl*****@NOSPAM.gazeta.pl> wrote in message
news:e5**************@TK2MSFTNGP09.phx.gbl...
From the code that you posted (which may have been pseudo code) it makes
it
look like it could be a static method. I wasn't sure. I was actually
thinking that if you're using objects then this would be a classic use of polymorphism. Or, for some reason, do you only have the name of the class as
a string?
There is 10 really diffrent classes but they have one common thing ;) The
delete funcion ;) And I chose to invoke it by late binding ;) Just to

learn something new.
Jarod

Mar 25 '06 #8
ASP.NET 2.0 - Safely Compile And Execute Source Code Dynamically

If I'm not mistaken, this can be adapted to 1.1 as well.

http://www.eggheadcafe.com/articles/...de_runtime.asp

--
Robbe Morris - 2004-2006 Microsoft MVP C#
Earn money answering .NET questions
http://www.eggheadcafe.com/forums/merit.asp

"Jarod" <bl*****@NOSPAM.gazeta.pl> wrote in message
news:e5**************@TK2MSFTNGP09.phx.gbl...
From the code that you posted (which may have been pseudo code) it makes
it
look like it could be a static method. I wasn't sure. I was actually
thinking that if you're using objects then this would be a classic use of
polymorphism. Or, for some reason, do you only have the name of the class
as
a string?


There is 10 really diffrent classes but they have one common thing ;) The
delete funcion ;) And I chose to invoke it by late binding ;) Just to
learn something new.
Jarod

Mar 26 '06 #9

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

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
0
by: Shawn Hogan | last post by:
Hi everyone, I've been trying to execute a control's private event code via reflection from another class with the goal of potentially doing some unit testing. The examples below are trying to...
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
0
by: Gustavo Arriola | last post by:
Hola a todos! Estoy intentando ejecutar un método usando Reflection. El código es el siguiente: public static void SoapHandler(Exception Error) { try { Type assemblyType;
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.