PC Review


Reply
Thread Tools Rate Thread

GetConstructor does not work when the constructor has ref paramete

 
 
=?Utf-8?B?YnVsbDIwMDBAbmV3c2dyb3VwLm5vc3BhbQ==?=
Guest
Posts: n/a
 
      22nd Jan 2007
here is the simplified code: the myCor is set to null when run the program.
but if remove the "ref" from the constructor's parameter, myCor gets a valid
value.

Please help!!! thanks.


Type myType = myAssembly.GetType("MyClass")
Type[] parameters = new Type[1];
parameters[0] = typeof(object);
ConstructorInfo myCor = myType .GetConstructor(parameters);

public class MyClass
{
public MyClass(ref object obj)
{
object o = obj;
}
}
 
Reply With Quote
 
 
 
 
Markus
Guest
Posts: n/a
 
      22nd Jan 2007
> here is the simplified code: the myCor is set to null when run the
> program. but if remove the "ref" from the constructor's parameter,
> myCor gets a valid value.


This is not a solution right now, but to be honest, I never have
required the ref-keyword in a constructor... I believe there is a better
way to handle this, seems to me as a week design (maybe I am wrong,
because I don't see the whole context).

Markus
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      22nd Jan 2007
(E-Mail Removed)am <(E-Mail Removed)> wrote:
> here is the simplified code: the myCor is set to null when run the program.
> but if remove the "ref" from the constructor's parameter, myCor gets a valid
> value.


As Markus said, it's very rare to need "ref" in a constructor (and I'd
need to see a good example to persuade me that it's a good idea) but
you can make GetConstructor work. Here's a short but complete example:

using System;
using System.Reflection;

public class MyClass
{
public MyClass(ref object obj)
{
Console.WriteLine ("Called");
}
}

class Test
{
static void Main()
{
Type type = typeof(MyClass);
Type[] parameterTypes = new Type[]
{ typeof(object).MakeByRefType() };
ConstructorInfo constructor = type.GetConstructor
(parameterTypes);

object[] args = new object[1];
constructor.Invoke(args);
}
}

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Markus
Guest
Posts: n/a
 
      22nd Jan 2007
> Type[] parameterTypes = new Type[]
> { typeof(object).MakeByRefType() };


Cool - thanks for the solution.

Markus
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bitmap constructor from stream doesnt work pomparoee@gmail.com Microsoft Dot NET Compact Framework 3 24th Apr 2009 04:01 AM
DataView Constructor RowFilter Doesn't Work? Spam Catcher Microsoft VB .NET 1 24th Jan 2008 04:47 PM
GetConstructor on Generic Classes. Undergrid Microsoft C# .NET 2 6th Mar 2007 12:19 PM
paramete web r query R..VENKATARAMAN Microsoft Excel Programming 0 24th Jan 2006 10:57 AM
GetConstructor method and private constructors Doug Microsoft C# .NET 2 9th Dec 2005 04:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:48 AM.