c#


C# static method from object


I have various objects of different types.
For all of them, I want to call a static method of their class. All the classes share the same method.
How can I call this static method without explicitly calling the class?
You could accomplish this by putting a method in each object that calls the corresponding static method. However, the fact that you want to do this suggests that your design might be able to be improved. If you'd tell us what you're trying to accomplish, someone may be able to suggest a better approach.
If these classes all extend the same base class, then calling the method on the base class will work.
For example:
public class Base
{
public static DoSomething()
{
//something
}
}
public class A: Base
{
}
public class B: Base
{
}
The following method calls execute the same code:
A.DoSomething();
B.DoSomething();
Base.DoSomething();
You want to call every method on each of the individual classes? You have to call them explicitly, referencing each class individually.
Does the static method for every class have the same common code? Put it into a static class for use by all of the other classes, or create one or more extension methods.
Are you looking for something like you have something like List<object> where all of the objects are guaranteed to have a static method named, say MethodX() ?
If so you could reflect on them, look for the method name, and execute that.
Either that or inheritance like the others mention (which would be the correct way to go).
If you need to have a specific implementation for each type, I don't think a static method is the right approach... Instead, you shoud define an interface implemented by all your classes. You can then call the instance method defined by the interface on each object :
public interface IDoSomething
{
void DoSomething();
}
public class A: IDoSomething
{
public void DoSomething()
{
// implementation for A
}
}
public class B: IDoSomething
{
public void DoSomething()
{
// implementation for B
}
}
Of course, if you don't need a specific implementation for each type, then you can just call Base.DoSomething (as explained by David)
I'm not sure what exactly you're trying to do. But using my imagination I come up with this implementation.
internal class Program
{
private static void Main(string[] args)
{
var staticMethodClasses = new List<StaticMethodClassBase>
{new ClassA(), new ClassB()};
foreach (StaticMethodClassBase item in staticMethodClasses)
{
Type t = item.GetType();
MethodInfo staticMethod =
t.GetMethod("DoSomething", BindingFlags.Static | BindingFlags.Public);
staticMethod.Invoke(null, null);
}
}
}
public abstract class StaticMethodClassBase
{
}
public class ClassA : StaticMethodClassBase
{
public static void DoSomething()
{
Console.WriteLine("Class A");
}
}
public class ClassB : StaticMethodClassBase
{
public static void DoSomething()
{
Console.WriteLine("Class B");
}
}

Related Links

Which MySQL version is my application using if I have both wamp as well as standalone MySQL installed separately?
strange behavior of Property functions
How to run the application from the other system
How to apply updates using Domain Service Class?
Compiling and accessing class at runtime [closed]
Memory leak with object initialized in a static method and using NSNotificationCenter
AutoMapper - Map using the same source and destination object types
C#: WMP, time stamp in milliseconds / tenth of a second
Match regex pattern when not inside a set of quotes (text spans multiple lines)
Conditional Statement Checking Null
Passing in func via where clause
how to delete dynamic textbox created in c#
Trying to login to a website clicking a button (Httpwebrequest - cookies)
Opening a specific text file
Application bar icons name
C# Instant Messenging

Categories

HOME
ssms
optimization
kendo-ui
netty
ide
pyspark
service
android-ndk
pandoc
foreach
clip-path
coding-style
core-data
kairosdb
element
swift3.1
hql
loss
civicrm
udeploy
ampscript
url-parameters
serenity-js
screen-scraping
devexpress-mvc
rvm
ps
libxml2
git-submodules
spring-data-gemfire
2checkout
tikz
jql
voyager
bootcamp
leaderboard
nsdateformatter
mediastream
lfe
ui-grid
tapply
susy
glide-image-library
paperclip
superclass
ocpjp
synchronized
spring-webflow-2
rivets.js
multi-targeting
totalview
rmq
iban
scrollmagic
ownership
foreground
init
key-value-store
system-integration
django-taggit
angular-i18n
archiva
pebble-js
android-progressbar
jersey-test-framework
github-for-mac
notice
cloud-storage
httpmodule
pymol
chrome-mobile
nexusdb
qxorm
random-access
register-allocation
gnip
oxygenxml
django-tests
reactjs-native
new-operator
connections
autofilter
surfaceview
azure-pack
maven-webstart-plugin
infinity.js
google-checkout
mail-gem
esi
shapado
certificate-revocation
event-propagation
mvcmailer
advanceddatagrid
jqzoom
zend-validate
digg
onunload
virtual-destructor
arraycollection
newspeak
mdd
vista64
stackless

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App