PHP function_exists / class_exists equivalent in C #

I am looking for an alternative to these two functions in asp.net/C#.

if(function_exists('foo')) { $returned = foo($bar); } if(class_exists('foo')) { $fooclass = new foo($bar); } 
+4
source share
4 answers
 Assembly assembly = Assembly.LoadFile("assemblyAddress"); bool containClass = assembly.GetTypes().Where(x=>x.Name == "ClassName").Count() > 0; bool containmethod = assembly.GetTypes().Where( x => x.GetMethods().Count(p => p.Name == "MethodName") > 0).Count() > 0; 
+2
source

Such functions are irrelevant since C # is a static language.
If the class does not exist, you will get a compile time error.

You can search for reflection.

+2
source

Such a thing does not translate from an interpreted and dynamically typed world into a fully compiled and strongly typed .NET world. However, there are other ways to get there, you might want to repeat the question of what effect you want to create.

0
source

All functions in C # must exist inside the class, so the first example does not apply in the context of global functions.

In the case of a class, you will know at compile time if it exists:

 MyClass x = new MyClass(); 

will return at compile time if MyClass not defined.

If you want to get a list of methods that belong to MyClass, you can use Reflection :

0
source

Source: https://habr.com/ru/post/1332529/


All Articles