简体   繁体   English

C#中静态类的反射

[英]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 但是当我访问该类的方法时,它显示了5种方法,但我仅创建了1种。额外的方法是

Write
ToString
Equals
GetHashCode
GetType

But i have created only the Write methods. 但是我只创建了Write方法。

One static methods can be in a static class but these extra 4 methods are not statics and from where they have drived. 一种静态方法可以在静态类中,但是这另外4种方法不是静态方法,也不是从何处驱动的。 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. 只能在静态类中声明静态成员-但就CLR而言,它只是另一个类,恰好只有静态成员,没有构造函数,并且既抽象又密封。 The CLR doesn't have the concept of a static class... so the class still inherits the instance members from object . CLR没有静态类的概念 ...因此该类仍从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 . C#中的每种类型都(直接或间接)继承自System.Object Thus inheriting Object 's methods ToString , GetHashCode , Equals and GetType . 因此,继承了Object的方法ToStringGetHashCodeEqualsGetType That is why you are seeing them while exploring all methods of ReflectionTest type object. 这就是为什么您在浏览ReflectionTest类型对象的所有方法时都看到它们的原因。 To get only static methods use this BindingFlags enum member: 要仅获取静态方法,请使用此BindingFlags枚举成员:

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

Those other methods are inherited from the Object base class. 这些其他方法是从Object基类继承的。

Pass BindingFlags.DeclaredOnly to GetMethods() to elide inherited methods. BindingFlags.DeclaredOnly传递给GetMethods()以取消继承的方法。

使用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#. 所有这些“附加”方法都来自object(alias) / Object ,它是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. 在C#的统一类型系统中,所有类型(预定义和用户定义的类型,引用类型和值类型)都直接或间接继承自Object。

You see Object methods (even if not static). 您会看到Object方法(即使不是静态的)。 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). 为了限制方法列表,您应该指定只希望使用BindingFlags.Static使用静态方法。您的类被标记为静态并不重要,我想出于与第一个.NET版本的兼容性原因,修饰符仅适用于编译器(您不能创建一个实例,依此类推)。

Static classes inherit from System.Object , and that's where you get these methods from. 静态类继承自System.Object ,从那里可以获取这些方法。 You can look at MethodInfo.DeclaringType to check. 您可以查看MethodInfo.DeclaringType进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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