简体   繁体   中英

Reflection For Static Class in C#

I have created a Static Class and used that in Reflection. But when i accessed the Methods of that class, its showing 5 methods but i have created only 1. The extra methods are

Write
ToString
Equals
GetHashCode
GetType

But i have created only the Write methods.

One static methods can be in a static class but these extra 4 methods are not statics and from where they have drived. What is the base class for that

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 an object of a static class to access those methods static class cannot have non static methods

Only static members can be declared in a static class - but as far as the CLR is concerned, it's just another class, which happens to only have static members, doesn't have a constructor, and is both abstract and sealed. The CLR doesn't have the concept of a static class... so the class still inherits the instance members from object .

This is a good example of why it's important to distinguish between language features, framework features and runtime features.

Every type in C# inherits (directly, or indirectly) from System.Object . Thus inheriting Object 's methods ToString , GetHashCode , Equals and GetType . That is why you are seeing them while exploring all methods of ReflectionTest type object. To get only static methods use this BindingFlags enum member:

type.GetMethods(System.Reflection.BindingFlags.Static)

Those other methods are inherited from the Object base class.

Pass BindingFlags.DeclaredOnly to GetMethods() to elide inherited methods.

使用BindingFlags时,必须显式指定所需的方法标志:

type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);

这些方法从object类派生而来,所有类都从object类派生而来。

All these "additional" methods come from object (alias) / Object , the base class everything in C#. Here's the quote:

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

You see Object methods (even if not static). To restrict your method list you should specify you want only static methods using BindingFlags.Static It doesn't matter your class is marked as static, I guess for compatibility reasons with first .NET versions that modifier is only for compilers (you can't create an instance and so on).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM