繁体   English   中英

C#中静态类的反射

[英]Reflection For Static Class in C#

我创建了一个静态类,并在反射中使用了它。 但是当我访问该类的方法时,它显示了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();            
        }
    }
}

但是如何创建静态类的对象来访问那些方法,静态类不能具有非静态方法

只能在静态类中声明静态成员-但就CLR而言,它只是另一个类,恰好只有静态成员,没有构造函数,并且既抽象又密封。 CLR没有静态类的概念 ...因此该类仍从object继承实例成员。

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

C#中的每种类型都(直接或间接)继承自System.Object 因此,继承了Object的方法ToStringGetHashCodeEqualsGetType 这就是为什么您在浏览ReflectionTest类型对象的所有方法时都看到它们的原因。 要仅获取静态方法,请使用此BindingFlags枚举成员:

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

这些其他方法是从Object基类继承的。

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

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

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

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

所有这些“附加”方法都来自object(alias) / Object ,它是C#中所有东西的基类。 这是报价:

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

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

静态类继承自System.Object ,从那里可以获取这些方法。 您可以查看MethodInfo.DeclaringType进行检查。

暂无
暂无

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

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