473,788 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Activator.Creat eInstance in C#

I am using CreateInstance to create an instance of a class and invoke
a function of that class. I really need it to execute in a single
line of code (long story, but I want to execute this code in the
command window on demand, without declaring any variables).

This code WORKS (but requires 2 lines of code and a variable
declaration):
Type typ = Type.GetTypeFro mProgID("MyDLLx .clsMyClass");
Activator.Creat eInstance(typ). mNoShow("Test") ;

This Code DOES NOT WORK:
Activator.Creat eInstance((Syst em.Type)Type.Ge tTypeFromProgID ("MyDLLx.clsMyC lass")))).mNoSh ow("Test");

The error, when executed from the command window is:
arguments do not match parameters for function ...

The error (compile), when coded in the application is:
\'object' does not contain a definition for 'mNoShow'

Any help is most appreciated.
Nov 16 '05 #1
3 12690
Type typ = Type.GetTypeFro mProgID("MyDLLx .clsMyClass");
Activator.Creat eInstance(typ). mNoShow("Test") ;

This Code DOES NOT WORK:
Activator.Creat eInstance((Syst em.Type)Type.Ge tTypeFromProgID ("MyDLLx.clsMyC lass")))).mNoSh ow("Test");

The error, when executed from the command window is:
arguments do not match parameters for function ...

The error (compile), when coded in the application is:
\'object' does not contain a definition for 'mNoShow'

Any help is most appreciated.


Are you sure these are verbatim lines? Neither one will work.(Although it
probably will in VB)


Nov 16 '05 #2
Doug,

Actually, neither line of code should work. The reason for this is that
the CreateInstance method returns an object. The only way I can see this
working is if it is in VB.

I would recheck the two lines you say that work, because they shouldn't.

Is it possible that the two lines are in a block that is swallowing the
exception?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Doug Riley" <dr****@platfor mdev.com> wrote in message
news:52******** *************** **@posting.goog le.com...
I am using CreateInstance to create an instance of a class and invoke
a function of that class. I really need it to execute in a single
line of code (long story, but I want to execute this code in the
command window on demand, without declaring any variables).

This code WORKS (but requires 2 lines of code and a variable
declaration):
Type typ = Type.GetTypeFro mProgID("MyDLLx .clsMyClass");
Activator.Creat eInstance(typ). mNoShow("Test") ;

This Code DOES NOT WORK:
Activator.Creat eInstance((Syst em.Type)Type.Ge tTypeFromProgID ("MyDLLx.clsMyC lass")))).mNoSh ow("Test");

The error, when executed from the command window is:
arguments do not match parameters for function ...

The error (compile), when coded in the application is:
\'object' does not contain a definition for 'mNoShow'

Any help is most appreciated.

Nov 16 '05 #3
If you remove the needless cast, and the two extra close parentheses, you
are left with:

Activator.Creat eInstance(Type. GetTypeFromProg ID("MyDLLx.clsM yClass")).mNoSh o
w("Test");

Which compiles (except for the error Daniel & Nicholas pointed out)

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Doug Riley" <dr****@platfor mdev.com> wrote in message
news:52******** *************** **@posting.goog le.com...
I am using CreateInstance to create an instance of a class and invoke
a function of that class. I really need it to execute in a single
line of code (long story, but I want to execute this code in the
command window on demand, without declaring any variables).

This code WORKS (but requires 2 lines of code and a variable
declaration):
Type typ = Type.GetTypeFro mProgID("MyDLLx .clsMyClass");
Activator.Creat eInstance(typ). mNoShow("Test") ;

This Code DOES NOT WORK:
Activator.Creat eInstance((Syst em.Type)Type.Ge tTypeFromProgID ("MyDLLx.clsMyC l
ass")))).mNoSho w("Test");
The error, when executed from the command window is:
arguments do not match parameters for function ...

The error (compile), when coded in the application is:
\'object' does not contain a definition for 'mNoShow'

Any help is most appreciated.

Nov 16 '05 #4

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

Similar topics

2
505
by: shmeian | last post by:
I have the following code which works fine. However I want to pass the object I'm instantiating a string for its constructor. I can't get the syntax right. Can someone give me an example of Activator.CreateInstance where they pass an argument with the object type? Assembly a = Assembly.GetExecutingAssembly(); Type type = a.GetType("MyAssemb.MyClass"); Object o = Activator.CreateInstance(type); MyClass myObj=...
0
5314
by: ka | last post by:
I'm implementing an IDesignerHost, for the CreateComponent method. The code below is quite standard. When loading a form, the CreateComponent works fine. However, when someone choose a control from the toolbox, the Activator couldn't create the component and returns null in CreateInstance. In what scenario, Activator.CreateInstance will be failed ?? I only create a System.Windows.Forms.Label. I've verified the type passed in is correct. ...
4
6715
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
12036
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 {
4
2721
by: Martin Maat | last post by:
Hi. I am using a COM component from managed code doing the following: Type type = Type.GetTypeFromCLSID(new Guid("B70FAAE6-4F85-480A-B1C5-DC9A6F175BFC"), serverMachineName, true); history = Activator.CreateInstance(type) as HistoryClass; This works on the local machine, no problem whatsoever. But when I run the client remotely, the cast in the second line of code shown above fails.
1
4765
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
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?
0
1434
by: Jon Skeet [C# MVP] | last post by:
On Apr 11, 8:40 am, Andrew <And...@discussions.microsoft.comwrote: 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.
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
9498
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
10175
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...
1
10112
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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
8993
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
7518
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
5399
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3675
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.