idos

Dear All

can someone help me, what is the different between System.Type and System.RuntimeType

assume:
Type[] array = new Type[]{typeof(string)};
Type type1 = typeof(Type);
Type type2 = array[0].GetType();
then why type1.Equals(type2) return false

THank you in advance.

Sorry for my poor English.



Re: Visual C# General Different between System.Type and System.RuntimeType

OmegaMan

Usage of type allows one at runtime to divine what an object is, look at its metadata. The usage of Equals is looking for exact matches. A better way to do your example is this

Code Snippet


Type[] typeArrays = new Type[2];

Type type1 = typeof(Type);
Type type2 = typeof(string);

typeArrays[0] = type1;
typeArrays[1] = type2;

// Prints
// Type 1 (System.Type) is not Type 2 (System.String)
Console.WriteLine("Type 1 ({0}) is {1} Type 2 ({2})",
typeArrays[0].ToString(),
(typeArrays[0].Equals(typeArrays[1]) string.Empty : "not"),
typeArrays[1].ToString());



When the array is filled, the first item of type Type is not the same as the second item of type String which Equals duitifully reports.






Re: Visual C# General Different between System.Type and System.RuntimeType

Mattias Sjogren

System.Type is an abstract base class and RuntimeType happens to be the class that implements all the functionality. AFAIK all Type objects are actually an instance of RuntimeType, but that's an implementation detail that could change in the future (note that RuntimeType isn't a public type so no guarantees about it).






Re: Visual C# General Different between System.Type and System.RuntimeType

Caddre

Actually System.Type is a child class of System.Reflection.MemberInfo abstract class and it is the main class of the Reflection subsystem used to access all types with the TypeOf operator. The main class used for type discovery including automated type discovery. I don't know System.RuntimeType and I am not sure if they are related because Reflection can fill a book.


http://msdn2.microsoft.com/en-us/library/system.type.aspx

http://msdn2.microsoft.com/en-us/library/system.reflection.memberinfo.aspx






Re: Visual C# General Different between System.Type and System.RuntimeType

idos

Maybe my sample is not very clear. What I am trying to do actually is to check the type of each element in the array to a type. I want to create a method to check whether every element of an array is a certain type. I cannot use 'is' keyword here since the type can be change. (as far as I know we cannot use 'is' keyword with a variable, please let me know if there is such a way to do so)

for example

object[] arrayInteger = { 1, 2, 3, 4};

and my method

checkType(arrayInteger, typeof(int)) will return true

object[] notPureInteger = { 1, "string", 2, 3};

checkType(notPureInteger, typeof(int)) will return false

now the problem is when I tried to check whether the element is of type System.Type.

I create object[] arrayType = { typeof(string), typeof(int), new ArrayList().GetType() };

checkType(arrayType, typeof(Type)) return false

or event when I use Type[] pureType = { typeof(string), typeof(int), new ArrayList().GetType() };

checkType(pureType, typeof(Type)) return false

my checkType method is as follows:

bool checkType(object[] array, Type type)

{

foreach(object element in array)

{

if(element.GetType().Equals(type) == false)

return false;

}

return true;

}

The method works (at least return the expected value) for integer and string type, but not System.Type type.

I try several different ways such as:

    • if( type.Equals(element) )
    • try{Convert.ChangeType(element as IConvertible, type);}

catch{return false;}

When I print its type by adding the following lines:

Console.WriteLine(element.GetType().ToString());

Console.WriteLine(type.ToString());

The output is System.RuntimeType and System.Type

So that is why I am asking the different between the two.






Re: Visual C# General Different between System.Type and System.RuntimeType

Thomas Danecker

System.Type is an abstract base class. The CLR has it's concrete implementation in the internal type System.RuntimeType. Because of this typeof(string).GetType() returns a RuntimeType but typeof(Type) returns a normal Type. Using the .Equals method does in fact an object.ReferenceEquals which returns false. To get the expecting results, you may use type.IsInstanceOfType(element). This will also return true if element is of a derived type. If you'd want to check for the exact type, the return-value of false of your method is desired result. You may also use checkType(arrayType, Type.GetType("System.RuntimeType")) to check for the RuntimeType.






Re: Visual C# General Different between System.Type and System.RuntimeType

idos

wow thank you for pointing this method out. I must have overlooked this method. Now Everything goes as expected. THank you. Big SmileBig Smile