473,767 Members | 7,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: activator classes n createInstance( )

On Apr 11, 8:40 am, Andrew <And...@discuss ions.microsoft. comwrote:
Hi,

I've looked at the Activator classes and I've come out with these samples of
code. However there is an error:

e.g. codes:
=======
string name = "ClassABC";

Type theMathType = Type.GetType("S ystem.Math");
Type testType = Type.GetType(na me);
Type testType2 = Type.GetType("n ame");

--both testType and testType2 gives "null".
Okay, that means you've either not given the full classname (including
namespace) or it's not in mscorlib or the currently executing
assembly, in which case you need to give assembly details as well.

The line with testType2 is explicitly trying to find a class called
"name". The string "name" and the variable called name are completely
independent things.
Object theObj2 = Activator.Creat eInstance(theMa thType); --Error: Cannot
create an abstract class.
That's natural - try compiling this:

Math m = new System.Math();

and you'll have the same kind of issue.
Object theObj3 = Activator.Creat eInstance(testT ype); --Error: Value
cannot be
null. Parameter name: type.
Object theObj4 = Activator.Creat eInstance(testT ype2); --Error: Value
cannot be
null. Parameter name: type.
Well yes - you've failed to find the type, so Activator.Creat eInstance
can't possibly create an instance of it.
I'm not very familiar with CreateInstance method etc ...
I just want to create an instance of "name" which is the name of the class
that I want to instantiate.
Is it in the same assembly as your calling code? If so, it's probably
just a case of putting the namespace on as well.

Jon
Jun 27 '08 #1
0 1433

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

Similar topics

4
6713
by: Terry | last post by:
I'm building some dll assemblies that have in them the implementation of an abstract class defined in a different assembly. I'm trying to create objects of the type defined in the dlls with "Activator.CreateInstance". Everything was working fine until I started to fill out the class def with some implementation. At some point the CreateInstance was failing with a "MissingMethodException - No parameterless constructor defined for this...
7
12032
by: hazz | last post by:
this is a repost with more concise code (well, for me) and better questions (I hope....) . given the following two classes, my intent is to use either Activator.CreateInstance or InvokeMember pass a token into the instantiated class DBPassword and return a string; ************************************** namespace DBPasswordProvider public class DBPassword {
3
5463
by: Rob Richardson | last post by:
Greetings! I am trying to make it possible for new derived classes of an object to be used by my application without rebuilding the application. The new classes will be made known to my application by storing the file name, assembly name and class name for them in a database. The code to create an instance of the object will look something like the following: Assembly asm = Assembly.LoadFrom(derivedFileName); Type derivedType =...
0
1231
by: MichaelSL | last post by:
When I build an executable which uses System.Activator.CreateInstance and execute it on my PC, which has Microsoft Visual Studio installed, the CreateInstance works just fine and the class constructor is called. But when I move this executable to another PC (even one with Microsoft Visual Studio) the execution proceeds all the way to the CreateInstance statement but hangs there with the constructor never executing. Any ideas? Is there...
1
4764
by: hazz | last post by:
this is a repost with a hopefully more clearly stated scenario and more concise questions at the end. given the following two classes, my intent is to use pass a token into the instantiated class DBPassword and return a string; ************************************** public class DBPassword { public DBPassword() { //Empty constructor
2
3359
by: jcomber | last post by:
Hi, I'd be grateful for some help... I'm trying to create a late bound object using: Activator.CreateInstance(Type, ConstructorParameters()) The constructor of the object takes a delegate as the only parameter. However, ConstructorParameters() is an object array and because a
1
11538
by: Johnny R | last post by:
Hello, I'm loading a Class from Assemly DLL using Activator.CreateInstance. That loaded Class is executed in a worker Thread with no loop. What actually happends when class is loaded using Activator.CreateInstance? If I create same class using Activator.CreateInstance many times will there be multiple instances of that same class created by Activator.CreateInstance?
1
1417
by: Dan Dorey | last post by:
My goal is to pass in an assembly and have the method return an instance of any classes that inherit from the generic type T. My problem is that I'm unable to cast from the result of Activator.CreateInstance(curType) to T even though curType is a concrete class of T (T is abstract in this case). I'm unsure why I can't make this cast. In another method where I don't have the restriction of T being a class, this approch works fine when T...
0
1297
by: =?Utf-8?B?QW5kcmV3?= | last post by:
Found it. string name = Properties.Settings.Default.ClassName.ToString(); //"myproject.myclass, myassembly" format. //name = "ABC.MyClass, Assem" ; Type t = Type.GetType(name); Object obj = Activator.CreateInstance(t); string result = t.InvokeMember("methodName",
0
9407
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10171
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10015
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9842
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8840
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7384
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5280
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5425
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2808
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.