Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 473
Kishore Kumar Avatar asked Mar 09 '12 11:03

Kishore Kumar


People also ask

What is the purpose of static class?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

What is reflection in C?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

What is static class in C?

A static class can only contain static data members, static methods, and a static constructor.It is not allowed to create objects of the static class. Static classes are sealed, means you cannot inherit a static class from another class. Syntax: static class Class_Name { // static data members // static method }

Why would you use reflection C#?

Reflection in C# is used to retrieve metadata on types at runtime. In other words, you can use reflection to inspect metadata of the types in your program dynamically -- you can retrieve information on the loaded assemblies and the types defined in them.


1 Answers

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.

like image 164
Jon Skeet Avatar answered Nov 02 '22 19:11

Jon Skeet