473,398 Members | 2,165 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,398 software developers and data experts.

Type.GetType on struct type

Hi,

I am wondering why 'Type.GetType' has a different behaviour if called
with a user defined struct or a .NET struct (i.e.
System.Drawing.Point).

Example:

using System;

namespace nsGetType
{
struct MyStruct
{
}

class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Type is [{0}]",
Type.GetType("System.Drawing.Point"));

Console.WriteLine("Type is [{0}]",
Type.GetType("nsGetType.MyStruct"));
}
}
}

Output:

Type is []
Type is [nsGetType.MyStruct]
Thanks for any help.

Marco.
Nov 16 '05 #1
2 7006
marco_segurini <ma***********@virgilio.it> wrote:
I am wondering why 'Type.GetType' has a different behaviour if called
with a user defined struct or a .NET struct (i.e.
System.Drawing.Point).


It doesn't, particularly (and being a struct has nothing to do with
it). It has the same behaviour in each case - it looks in the calling
assembly and mscorlib, unless the assembly details are also provided.
In this case, System.Drawing.Point isn't in the calling assembly or
mscorlib, so it returns null, as documented. It would do the same for a
user-defined type in a different assembly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Thanks a lot Jon.
marco_segurini <ma***********@virgilio.it> wrote:
I am wondering why 'Type.GetType' has a different behaviour if called
with a user defined struct or a .NET struct (i.e.
System.Drawing.Point).


It doesn't, particularly (and being a struct has nothing to do with
it). It has the same behaviour in each case - it looks in the calling
assembly and mscorlib, unless the assembly details are also provided.
In this case, System.Drawing.Point isn't in the calling assembly or
mscorlib, so it returns null, as documented. It would do the same for a
user-defined type in a different assembly.


At the moment I am looking the MSDN examples on how to implement
strongly typed CollectionBase and DictionaryBase classes. The
OnValidate is implemented using Type.GetType:

public class Int16Collection : CollectionBase {

....

protected override void OnValidate( Object item) {
if ( item.GetType() != Type.GetType("System.Drawing.Point") )
throw new ArgumentException( "value must be of type
System.Drawing.Point", "item" );
}

}

Now I know that this in not a general method.
I have replace that member-function with this:

protected override void OnValidate( Object item )
{
if (!(item is System.Drawing.Point))
throw new ArgumentException( "item must be of type
System.Drawing.Point.", "item" );
}

that in this case works.

Is this a general method to verify the object type?

Marco.
Nov 16 '05 #3

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

Similar topics

4
by: Tamir Khason | last post by:
How can I set the type of the object added to ArrayList (type of Array List Members) Here is the code: protected ArrayList tabs = new ArrayList(); public ArrayList Tabs {
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
2
by: S. Justin Gengo | last post by:
Hi, I've created a component that allows me to store database information for various types of databases my company uses. It uses a collection for each type of database. Everything is working...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
16
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
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}"...
1
by: Sky | last post by:
Yesterday I was told that GetType(string) should not just be with a Type, but be Type, AssemblyName. Fair enough, get the reason. (Finally!). As long as it doesn't cause tech support problems...
4
by: Brette.Net | last post by:
Hello All, I was doing a little messing around with some type conversion they other day. Code is below. struct ValueStruct { int a; public ValueStruct(int inA)
5
by: not_a_commie | last post by:
It seems that the only way to construct a struct from a type is to use Activator.CreateInstance. Is that true? Can anyone improve (performance-wise) upon this function below: /// <summary>...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.