Reflection for a static class in C #

I created a Static Class and used this in Reflection. But when I turned to the methods of this class, it showed 5 methods, but I created only 1. Additional methods:

Write ToString Equals GetHashCode GetType 

But I only created Write methods.

One static method may be in a static class, but these additional 4 methods are not static and from there. What is the base class for this

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ReflectionDemo { static class ReflectionTest { public static int Height; public static int Width; public static int Weight; public static string Name; public static void Write() { Type type = typeof(ReflectionTest); //Get type pointer FieldInfo[] fields = type.GetFields(); //obtain all fields MethodInfo[] methods = type.GetMethods(); Console.WriteLine(type); foreach (var item in methods) { string name = item.Name; Console.WriteLine(name); } foreach (var field in fields) { string name = field.Name; //(null); //Get value object temp = field.GetValue(name); if (temp is int) //see if it is an integer { int value = (int)temp; Console.Write(name); Console.Write("(int) = "); Console.WriteLine(value); } else if (temp is string) { string value = temp as string; Console.Write(name); Console.Write("(string) = "); Console.WriteLine(value); } } } } class Program { static void Main(string[] args) { ReflectionTest.Height = 100; ReflectionTest.Width = 50; ReflectionTest.Weight = 300; ReflectionTest.Name = "Perl"; ReflectionTest.Write(); Console.ReadLine(); } } } 

But how to create a static class object to access these methods, a static class cannot have non-static methods

+4
source share
8 answers

Each type in C # inherits (directly or indirectly) from System.Object . Thus, inheriting the methods Object ToString , GetHashCode , Equals and GetType . That is why you look at them when studying all the methods of an object of type ReflectionTest . To get only static methods, use this BindingFlags enum element:

 type.GetMethods(System.Reflection.BindingFlags.Static) 
+8
source

Only static members can be declared in a static class, but with respect to the CLR, it is just another class that has only static members, has no constructor, and is abstract and sealed. The CLR does not have the concept of a static class ... so the class still inherits instance members from object .

This is a good example of why it is important to distinguish between language functions, framework functions, and execution capabilities.

+21
source

These other methods are inherited from the base class Object .

Pass BindingFlags.DeclaredOnly to GetMethods() to remove the inherited methods.

+3
source

These methods are derived from the object class, from which all classes are initially derived.

+1
source

All these additional methods are taken from the object (alias) / Object , the base class is all in C #. Here is a quote:

In a C # unified type system, all types predefined and user-defined reference types and value types inherit directly or indirectly from Object.

+1
source

When using BindingFlags, you must explicitly specify the required method flags:

 type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static); 
+1
source

You see Object methods (even if not static). To limit the list of methods, you must indicate that you want to use only static methods using BindingFlags.Static It does not matter that your class is marked as static, I assume that, for reasons of compatibility with the first versions of .NET, this modifier is intended only for compilers ( you cannot create an instance, etc.).

0
source

Static classes inherit from System.Object and where you get these methods from. You can look at MethodInfo.DeclaringType to check.

0
source

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


All Articles