【问题标题】:Reflection For Static Class in C#C#中静态类的反射
【发布时间】:2012-03-26 20:25:03
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net reflection static


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 但是我不能像静态类'ReflectionTest.GetType()'那样从我的类中获取这些方法
      • 我们也不能为静态类创建对象。以及为什么不能在静态类中继承静态成员
      • 对,那是因为这些方法不是静态的。但是,您正在探索 Type 对象,您会看到静态和非静态方法。
      • 那么如何访问这些方法
      • 这些方法无论如何都不属于ReflectionTest 类。这些是您正在创建的Type type = typeof(ReflectionTest) 对象的方法。它们不存在于ReflectionTest 类的上下文中,您根本无法从中访问它们。 Write 方法完全不同 - 它是 ReflectionTest 声明的一部分,您可以像 ReflectionTest.Write 一样简单地访问它。
      【解决方案3】:

      那些其他方法继承自 Object 基类。

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

      【讨论】:

        【解决方案4】:

        这些方法派生自 object 类,所有类都从该类本地派生。

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

            使用 BindingFlags 时,您必须明确指定所需的方法标志:

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

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                【解决方案8】:

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

                【讨论】:

                  猜你喜欢
                  • 2010-11-22
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-10-30
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-10-20
                  相关资源
                  最近更新 更多