13

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

Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154

8 Answers8

27

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
14

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)
Richard
  • 106,783
  • 21
  • 203
  • 265
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • 1
    But i am not able to get those method from my class like we get for Static class 'ReflectionTest.GetType()' – Kishore Kumar Mar 09 '12 at 11:35
  • And we cannot also create a object for Static class. and how come not static members can be inherited in Static Class – Kishore Kumar Mar 09 '12 at 11:36
  • Right, that is because these methods are not static. However you are exploring the Type object, and you see both static and not-static methods. – Andrei Mar 09 '12 at 11:39
  • then how to access those methods – Kishore Kumar Mar 09 '12 at 11:44
  • 1
    These methods do not belong to the `ReflectionTest` class in any way. These are the methods of `Type type = typeof(ReflectionTest)` object that you are creating. They do not exist in a context of `ReflectionTest` class, and you cannot access them from it at all. `Write` method is totally a different story - it is a part of `ReflectionTest` declaration, and you can access it as simply as `ReflectionTest.Write`. – Andrei Mar 09 '12 at 11:52
  • if my class cannot access those method, then why this type.GetMethods() showing those method name – Kishore Kumar Mar 09 '12 at 11:57
  • Because this is not the class that you are exploring - this is the reflection object of class `Type` that represents your class `ReflectionTest`. Besides information about your class it also contains common information (including methods) all objects contain. – Andrei Mar 09 '12 at 11:59
3

Those other methods are inherited from the Object base class.

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

AKX
  • 152,115
  • 15
  • 115
  • 172
1

When using BindingFlags you must explicitly specify the desired method flags:

type.GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
Christiaan Wevers
  • 422
  • 1
  • 6
  • 12
1

These methods are derived from the object class from which all classes natively derive.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
1

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.

Patryk Ćwiek
  • 14,078
  • 3
  • 55
  • 76
0

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).

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
0

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

Botz3000
  • 39,020
  • 8
  • 103
  • 127