473,378 Members | 1,162 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,378 software developers and data experts.

Calling constructor from a System.Type?

What I want to do is this:

Object1[] list = (Object1[])this.MyMethod(typeOf(Object1), "Object1",
"string1", "string2");
and
Object2[] list = (Object2[])this.MyMethod(typeOf(Object2), "Object2",
"string1");
But I can't find how to call a constructor from a type
// Do I have to return object[]?
public object[] MyMethod(Type t, string name, params object[] spParams)
{
DataReader dr = Data.Get(name, spParams);

retVal = new t.Construct(dr); // <-- THIS LINE NEEDS TO CALL CONSTRUCTOR

return (object[])retVal;

}
I've tried msdn, but can't find what I'm looking for
--
Daisy The Cow
Nov 15 '05 #1
4 1920

If you want to create an instance of the type t (and at the same time
call one of its constructors), the easiest way is to use
Activator.CreateInstance().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
Try using
Object retVal = Activator.CreateInstance( t, new object[] { /*params to
constructor go here */ dr });

Or you can use the .Invoke method on the type object you have... check the
bindingflags to call the constructor
"Daisy" <da***@nospam.oops> wrote in message
news:bu**********@linux01.dannytuppeny.com...
What I want to do is this:

Object1[] list = (Object1[])this.MyMethod(typeOf(Object1), "Object1",
"string1", "string2");
and
Object2[] list = (Object2[])this.MyMethod(typeOf(Object2), "Object2",
"string1");
But I can't find how to call a constructor from a type
// Do I have to return object[]?
public object[] MyMethod(Type t, string name, params object[] spParams)
{
DataReader dr = Data.Get(name, spParams);

retVal = new t.Construct(dr); // <-- THIS LINE NEEDS TO CALL CONSTRUCTOR
return (object[])retVal;

}
I've tried msdn, but can't find what I'm looking for
--
Daisy The Cow

Nov 15 '05 #3
"Philip Rieck" <st***@mckraken.com> wrote in message
news:ec**************@TK2MSFTNGP11.phx.gbl...
Object retVal = Activator.CreateInstance( t, new object[] { /*params to
constructor go here */ dr });


Cool, I'll give it a try :)
--
Daisy The Cow
Nov 15 '05 #4
Daisy <da***@nospam.oops> wrote:

<snip>
But I can't find how to call a constructor from a type


As well as Activator.CreateInstance, you could also call GetConstructor
on the Type, and then Invoke on the Constructor.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
1
by: Curt | last post by:
Hi, I have, in vain, tried to create a com object from a remote server. Setup; This is the code that fails. Projekt.ClassName _BOMsg =...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
3
by: RC | last post by:
Hi, I have a dll written by vb6. AFunction(ByRef o as Variant) I want to use it in csharp by System.Reflection. Assembly asm = Assembly.LoadFrom("Lib.dll"); Type t =...
5
by: Bennett Haselton | last post by:
I've noticed that if you enter the following code in the codebehind page for an .aspx page, it won't compile because the call to Trace.Write() is not valid except in methods of a class derived from...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
0
by: BornTOCode | last post by:
Hello, I am attempting to call a (Delphi) win32 DLL from a Delphi.Net webservice. I am using a slightly modified version of the hello world webservice that comes with Delphi 2006. The DLL...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.