C# C语言中静态类的反射#

C# C语言中静态类的反射#,c#,.net,reflection,static,C#,.net,Reflection,Static,我创建了一个静态类,并在反射中使用它。 但当我访问该类的方法时,它显示了5个方法,但我只创建了1个。 额外的方法是 Write ToString Equals GetHashCode GetType 但是我只创建了Write方法 一个静态方法可以在一个静态类中,但这4个额外的方法不是静态的,它们是从哪里驱动的。它的基类是什么 using System; using System.Collections.Generic; using System.Linq; using System.Text;

我创建了一个静态类,并在反射中使用它。 但当我访问该类的方法时,它显示了5个方法,但我只创建了1个。 额外的方法是

Write
ToString
Equals
GetHashCode
GetType
但是我只创建了Write方法

一个静态方法可以在一个静态类中,但这4个额外的方法不是静态的,它们是从哪里驱动的。它的基类是什么

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();            
        }
    }
}
但是如何创建静态类的对象来访问这些方法呢
静态类不能有非静态方法

这些方法是从所有类本机派生的
对象
类派生的。

其他方法是从基类继承的


BindingFlags.DeclaredOnly
传递给
GetMethods()
以删除继承的方法。

您将看到
对象
方法(即使不是静态的)。要限制您的方法列表,您应该指定您只需要使用BindingFlags的静态方法。静态将您的类标记为静态并不重要,我想出于与第一个.NET版本兼容的原因,修饰符仅用于编译器(您无法创建实例等)。

所有这些“附加”方法来自/,C#中的基类。 以下是报价:


在C#的统一类型系统中,所有类型(预定义和用户定义的、引用类型和值类型)都直接或间接地从Object继承。

静态类继承自
system.Object
,这就是这些方法的来源。您可以查看
MethodInfo.DeclaringType
进行检查。

静态类中只能声明静态成员-但就CLR而言,它只是另一个类,碰巧只有静态成员,没有构造函数,并且是抽象和密封的。CLR没有静态类的概念。。。因此,该类仍然从
对象
继承实例成员

这是一个很好的例子,说明了为什么区分语言特性、框架特性和运行时特性很重要。

C#中的每种类型都(直接或间接地)继承自
System.Object
。因此继承
对象的方法
ToString
GetHashCode
等于
GetType
。这就是为什么您在探索
ReflectionTest
type对象的所有方法时会看到它们。要仅获取静态方法,请使用此
BindingFlags
enum成员:

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

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

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

但我不能像静态类“ReflectionTest.GetType()”那样从类中获取这些方法,而且我们也不能为静态类创建对象。为什么非静态成员可以在静态ClassRight中继承,这是因为这些方法不是静态的。但是,在探索类型对象时,您会看到静态方法和非静态方法。那么如何访问这些方法这些方法在任何方面都不属于
ReflectionTest
类。这些是您正在创建的
Type=typeof(ReflectionTest)
对象的方法。它们不存在于
ReflectionTest
类的上下文中,您根本无法从该类中访问它们
Write
方法是完全不同的—它是
ReflectionTest
声明的一部分,您可以像
ReflectionTest.Write
一样简单地访问它。