How do you get the original type from a RuntimeType???

G

Guest

I have an application that is being passed objects which could either be an instance or a Type in the case of a Static Class

When you do the GetType on the object that was originally a Static class it gives you a type of "System.RuntimeType" as opposed to the orignal Static Class Typ

How do you get access to what type the the Orginal Static Class type was from the RuntimeType??

//Eg Routine......
Object[] objects = new Object[3]
Class1 class1 = new Class1("Class1")
objects[0] = class1
objects[1] = typeof(StaticClass)
objects[2] = typeof(StaticClass).FullName
//objects[1] = (Object)StaticClass

foreach (object o in objects

tr

Type type = o.GetType()
this.richTextBox1.Text += type.FullName+NL

if (type.FullName == "System.RuntimeType"

//??????? Here is what I need....How to Get the Orignal Static Class back????? from a
//System.RuntimeType???

else if (type.FullName == "System.String"

Type originalType = Type.GetType((string) o)
this.richTextBox1.Text += originalType.FullName+NL


catch (Exception ex

this.richTextBox1.Text += ex.Message+NL




******** Complete sample code Listin
using System
using System.Drawing
using System.Collections
using System.ComponentModel
using System.Windows.Forms
using System.Data

namespace MJBStaticTypesInOb

public class Form1 : System.Windows.Forms.For

private System.Windows.Forms.Button button1
private System.Windows.Forms.RichTextBox richTextBox1
private System.ComponentModel.Container components = null

public Form1(

InitializeComponent()


/// <summary
/// Clean up any resources being used
/// </summary
protected override void Dispose( bool disposing

if( disposing

if (components != null)

components.Dispose()


base.Dispose( disposing )


#region Windows Form Designer generated cod
/// <summary
/// Required method for Designer support - do not modif
/// the contents of this method with the code editor
/// </summary
private void InitializeComponent(

this.button1 = new System.Windows.Forms.Button()
this.richTextBox1 = new System.Windows.Forms.RichTextBox()
this.SuspendLayout()
//
// button
//
this.button1.Location = new System.Drawing.Point(32, 96)
this.button1.Name = "button1"
this.button1.TabIndex = 0
this.button1.Text = "button1"
this.button1.Click += new System.EventHandler(this.button1_Click)
//
// richTextBox
//
this.richTextBox1.Location = new System.Drawing.Point(24, 152)
this.richTextBox1.Name = "richTextBox1"
this.richTextBox1.Size = new System.Drawing.Size(208, 96)
this.richTextBox1.TabIndex = 1
this.richTextBox1.Text = "richTextBox1"
//
// Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13)
this.ClientSize = new System.Drawing.Size(292, 266)
this.Controls.Add(this.richTextBox1)
this.Controls.Add(this.button1)
this.Name = "Form1"
this.Text = "Form1"
this.ResumeLayout(false)


#endregio

/// <summary
/// The main entry point for the application
/// </summary
[STAThread
static void Main()

Application.Run(new Form1())


private void button1_Click(object sender, System.EventArgs e

string NL = Environment.NewLine
this.richTextBox1.Text = string.Empty

Object[] objects = new Object[3]
Class1 class1 = new Class1("Class1")
objects[0] = class1
objects[1] = typeof(StaticClass)
objects[2] = typeof(StaticClass).FullName
//objects[1] = (Object)StaticClass

foreach (object o in objects

tr

Type type = o.GetType()
this.richTextBox1.Text += type.FullName+NL

if (type.FullName == "System.RuntimeType"

//How to Get the Orignal Static Class back????? from a
//System.RuntimeType???

else if (type.FullName == "System.String"

Type originalType = Type.GetType((string) o)
this.richTextBox1.Text += originalType.FullName+NL


catch (Exception ex

this.richTextBox1.Text += ex.Message+NL




public class StaticClas

public static string Name = "StaticClass"

static StaticClass(){

public class Class

public string Name = "InstanceClass"
public Class1(string name)
{
Name = name;
}
}
}
 
V

Vijaye Raji

You are getting the type of the type object. Instead try this:

foreach (object o in objects)
{
try
{
Type type = o is Type ? (Type) o : o.GetType();
this.richTextBox1.Text += type.FullName+NL;

if (type.FullName == "System.RuntimeType")
{
}
....
....

-vJ

Mike in Paradise said:
I have an application that is being passed objects which could either be an
instance or a Type in the case of a Static Class.

When you do the GetType on the object that was originally a Static class
it gives you a type of "System.RuntimeType" as opposed to the orignal
Static Class Type

How do you get access to what type the the Orginal Static Class type was
from the RuntimeType???

//Eg Routine.......
Object[] objects = new Object[3];
Class1 class1 = new Class1("Class1");
objects[0] = class1;
objects[1] = typeof(StaticClass);
objects[2] = typeof(StaticClass).FullName;
//objects[1] = (Object)StaticClass;

foreach (object o in objects)
{
try
{
Type type = o.GetType();
this.richTextBox1.Text += type.FullName+NL;

if (type.FullName == "System.RuntimeType")
{
//??????? Here is what I need....How to Get the Orignal Static Class
back????? from a
//System.RuntimeType????
}
else if (type.FullName == "System.String")
{
Type originalType = Type.GetType((string) o);
this.richTextBox1.Text += originalType.FullName+NL;
}
}
catch (Exception ex)
{
this.richTextBox1.Text += ex.Message+NL;
}
}



******** Complete sample code Listing
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MJBStaticTypesInObj
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 152);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(208, 96);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "richTextBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string NL = Environment.NewLine;
this.richTextBox1.Text = string.Empty;

Object[] objects = new Object[3];
Class1 class1 = new Class1("Class1");
objects[0] = class1;
objects[1] = typeof(StaticClass);
objects[2] = typeof(StaticClass).FullName;
//objects[1] = (Object)StaticClass;

foreach (object o in objects)
{
try
{
Type type = o.GetType();
this.richTextBox1.Text += type.FullName+NL;

if (type.FullName == "System.RuntimeType")
{
//How to Get the Orignal Static Class back????? from a
//System.RuntimeType????
}
else if (type.FullName == "System.String")
{
Type originalType = Type.GetType((string) o);
this.richTextBox1.Text += originalType.FullName+NL;
}
}
catch (Exception ex)
{
this.richTextBox1.Text += ex.Message+NL;
}
}
}
}
public class StaticClass
{
public static string Name = "StaticClass";

static StaticClass(){}
}
public class Class1
{
public string Name = "InstanceClass";
public Class1(string name)
{
Name = name;
}
}
}
 
V

Vijaye Raji

OK. I think I understood the question wrong. You want to get back an
instance of a class that you never created (StaticClass) in the first place.
Whatever you're trying to do, you should look into Reflection, specifically
MethodInfo class.

HTH

-vJ

Vijaye Raji said:
You are getting the type of the type object. Instead try this:

foreach (object o in objects)
{
try
{
Type type = o is Type ? (Type) o : o.GetType();
this.richTextBox1.Text += type.FullName+NL;

if (type.FullName == "System.RuntimeType")
{
}
...
...

-vJ

Mike in Paradise said:
I have an application that is being passed objects which could either be
an instance or a Type in the case of a Static Class.

When you do the GetType on the object that was originally a Static class
it gives you a type of "System.RuntimeType" as opposed to the orignal
Static Class Type

How do you get access to what type the the Orginal Static Class type was
from the RuntimeType???

//Eg Routine.......
Object[] objects = new Object[3];
Class1 class1 = new Class1("Class1");
objects[0] = class1;
objects[1] = typeof(StaticClass);
objects[2] = typeof(StaticClass).FullName;
//objects[1] = (Object)StaticClass;

foreach (object o in objects)
{
try
{
Type type = o.GetType();
this.richTextBox1.Text += type.FullName+NL;

if (type.FullName == "System.RuntimeType")
{
//??????? Here is what I need....How to Get the Orignal Static Class
back????? from a
//System.RuntimeType????
}
else if (type.FullName == "System.String")
{
Type originalType = Type.GetType((string) o);
this.richTextBox1.Text += originalType.FullName+NL;
}
}
catch (Exception ex)
{
this.richTextBox1.Text += ex.Message+NL;
}
}



******** Complete sample code Listing
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MJBStaticTypesInObj
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(32, 96);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(24, 152);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(208, 96);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "richTextBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
string NL = Environment.NewLine;
this.richTextBox1.Text = string.Empty;

Object[] objects = new Object[3];
Class1 class1 = new Class1("Class1");
objects[0] = class1;
objects[1] = typeof(StaticClass);
objects[2] = typeof(StaticClass).FullName;
//objects[1] = (Object)StaticClass;

foreach (object o in objects)
{
try
{
Type type = o.GetType();
this.richTextBox1.Text += type.FullName+NL;

if (type.FullName == "System.RuntimeType")
{
//How to Get the Orignal Static Class back????? from a
//System.RuntimeType????
}
else if (type.FullName == "System.String")
{
Type originalType = Type.GetType((string) o);
this.richTextBox1.Text += originalType.FullName+NL;
}
}
catch (Exception ex)
{
this.richTextBox1.Text += ex.Message+NL;
}
}
}
}
public class StaticClass
{
public static string Name = "StaticClass";

static StaticClass(){}
}
public class Class1
{
public string Name = "InstanceClass";
public Class1(string name)
{
Name = name;
}
}
}
 
G

Guest

Thanks..That did it.... Seems obvious once you see the answer but I have been search for this for a couple of hours..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top