473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Gettype() returning a null

Hi,

I have an .aspx page that needs to instantiate an object whose type in
not known until runtime. The definition of said object resides in
/App_code/CMS.cs

CMS.cs
==================
namespace CMS{
public interface IWindmill{
string emit
{get;}
}

public class MyObject : IWindmill{
string html;

public MyObject(){
html = "Hello World"
}

public string emit{
get { return html; }
}
}

In my webpage, I am trying to instantiate myObject dynamically, like
so:

Default.aspx.cs
============
protected void Page_Load(object sender, EventArgs e){
Type t = Type.GetType("CMS.MyObject"); //This object will vary and
won't be determined until runtime
CMS.IWindmill myObj = (CMS.IWindmill)Activator.CreateInstance(t);

MethodInfo m = myObj.GetType().GetMethod("get_emit");
if (m != null){
string s = (string)m.Invoke(myObj, null);
Response.Write(s);
}
}

My Type t will return null unless I include the CMS namespace in the
Default.aspx.cs file. Is there a way for me to return the proper value
if the object is defined in an external file? I understand I could
package the CMS namespace in an assembly and reference it through its
qualified name, but is there an easier and more direct way to get the
type in an external .cs file?

Regards,

Chris

Dec 5 '06 #1
1 2031
<ha******@hotmail.comwrote:

<snip>
My Type t will return null unless I include the CMS namespace in the
Default.aspx.cs file. Is there a way for me to return the proper value
if the object is defined in an external file? I understand I could
package the CMS namespace in an assembly and reference it through its
qualified name, but is there an easier and more direct way to get the
type in an external .cs file?
Well, it will certainly be in *an* assembly by the time it's running.
Will you always want types from the same assembly, and do you know
*one* of those types at compile-time? If so, use typeof(...) to get
that type, then get the Assembly reference from that, then call
Assembly.GetType.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 5 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Suresh | last post by:
Hi, I have an C# CSEXE.exe (CSexe.cs) and a CSDll.dll (CSdll.cs). exe is compiled with a reference to dll. Calling for the class Type defined in CSDLL.dll using Type.GetType(...
6
by: Marco | last post by:
Hi. I am trying to get a type object using GetType. For this, I use: Type type = Type.GetType("System.Windows.Forms.TextBox"); But after this line, when I check, type is null. What am I...
8
by: ron | last post by:
Hi, I am currently using the base class GetType() in the following way and was woundering if there was a different way of looking at types and doing a comparison for equality. Thanks Ron ...
3
by: Jimi | last post by:
What am I doing wrong? I'm trying to get a type object for a type which is not currently loaded in my project (otherwise I'd use "typeof"). System.Type oType =...
1
by: hazz | last post by:
"Value cannot be null.\r\nParameter name: type" is the exception thrown after the CreateInstance method below. Type t = Type.GetType(GetConfigValue("PasswordProvider")); IPasswordProvider ppdr=...
1
by: Nicolas | last post by:
I want to return the proper Null to the SQL Database if my property is nothing Also I thought about creating a function so I don't have to put so many if statement for each property for each...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
2
by: Carlos Rodriguez | last post by:
I have the following function in C#public void Undo(IDesignerHost host) { if (!this.componentName.Equals(string.Empty) && (this.member != null)) { IContainer container1 = (IContainer)...
8
by: Martin Eckart | last post by:
Hi folks, Who can explain me why the following expression does not result in getting the correct type, but null: Type t = Type.GetType("System.Xml.XmlReader"); For "System.String" it works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.