473,498 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type.GetType issues

I have the typename of a form I wish to to load dynamically
(example:MyCompany.Client.Win.Security.Detail.SysU serDetailForm).
However, when I call Type.GetType(typeName) it return null I am assuming
since I did not use the full name which is not available. I wrote the below
method to return the Type but I have some concerns with it.

First is there an eaiser way to do this that I am missing. Second, I am
concerned about the Assembly.Load(assemblyName); call am I loading the
referenced assembly or another instance of the same assembly?

Thanks

/// <summary>
/// Searches all Types in this and referenced Assemblies for the
/// specified type. If the type is not found FindType returns null.
/// If the type is found in more thatn one Assmebly FindType raises
/// an exception.
/// </summary>
/// <param name="typeName">Name of the type.</param>
/// <returns>Returns the Type or null if it cannot be
found</returns>
public static Type FindType(string typeName)
{
Type result = null;
int findCount = 0;

// Iterrate through the local assembly first.
Assembly root = Assembly.GetExecutingAssembly();
foreach (Type type in root.GetTypes())
{
if (type.FullName.Equals(typeName))
{
if (result == null)
result = type;
findCount++;
}
}

// Iterrate through all referenced assemblies
foreach (AssemblyName assemblyName in
root.GetReferencedAssemblies())
{
Assembly referencedAssembly = Assembly.Load(assemblyName);
foreach (Type type in referencedAssembly.GetTypes())
{
if (type.FullName.Equals(typeName))
{
if (result == null)
result = type;
findCount++;
}
}
}

if (findCount 0)
throw new Exception(string.Format("An ambiguous type error
has occurred in the method FindType. More that one Type exists with the
name {0}.", typeName));

return result;
}
Feb 23 '07 #1
1 3522
Jamey,
>First is there an eaiser way to do this that I am missing.
Where do you get the type name from? Can't you store the assembly name
with the type name?
>Second, I am
concerned about the Assembly.Load(assemblyName); call am I loading the
referenced assembly or another instance of the same assembly?
The runtime will only load the assembly once (per appdomain). If it's
already loaded you'll get a reference back to the existing one.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 23 '07 #2

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

Similar topics

4
2536
by: Chris Bower | last post by:
Reposted from aspnet.buildingcontrols: Ok, I've got a bunch of derived controls that all have a property Rights of type Rights (Rights is an Enumerator). I wrote a custom TypeConverter so that I...
3
12941
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
3286
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
5158
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...
13
12358
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
7
7796
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
2133
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...
3
10214
by: Julie | last post by:
Here's the scenario (public attributes, etc. omitted for brevity): class Base { } class Derived : Base { }
1
2320
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
0
7121
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
7197
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...
1
6881
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
5456
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
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.