Getting type of ref parameter

M

marc

hi there,

i try to do a little generic style of programing in c#. the situation
is like this:

class MyObj1
{
}

class MyObj2 : MyClass1
{
}

......

void EnsureObjectExists(ref MyObj1 myObj)
{
if (myObj == null)
{
Type myObjType = typeof(myObj);
myObj = myObjType.CreateInstance();
}
}

....

MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);

Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)

Cheers,
Marc
 
M

Marc Gravell

void EnsureObjectExists<T>(ref T obj) where T : new()
{
if (obj== null) obj= new T();
}

Note that the compiler can infer the <T> in this case, so:

SomeClass test = null;
EnsureObjectExists(ref test);

should work fine.

Marc
 
T

Tom Spink

hi there,

i try to do a little generic style of programing in c#. the situation
is like this:

class MyObj1
{
}

class MyObj2 : MyClass1
{
}

.....

void EnsureObjectExists(ref MyObj1 myObj)
{
if (myObj == null)
{
Type myObjType = typeof(myObj);
myObj = myObjType.CreateInstance();
}
}

...

MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);

Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)

Cheers,
Marc

Hi Marc,

With your current implementation, it's not possible to infer the type of
object within the EnsureObjectExists method, if you're just passing in
null, then the method has no way of knowing what type it is.

You could alter your method to use generics, if you are using .NET 2.0:

///
private void EnsureObjectExists<T> ( ref T myObj ) where T : MyObj1, new()
{
if ( myObj == null )
myObj = new T();
}
///

Then use it as follows:

///
MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists<MyObj1>( ref obj1 );
EnsureObjectExists<MyObj2>( ref obj2 );
///
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

This will not work:
Type myObjType = typeof(myObj);

It will always return MyObj1 cause typeof is evaluated at compile time.


What you need to do is
if (myObj == null)
{
Type myObjType = myObj.GetType();

This will be evaluated at runtime and will return the correct type.
 
T

Tom Spink

Hi,

This will not work:
Type myObjType = typeof(myObj);

It will always return MyObj1 cause typeof is evaluated at compile time.


What you need to do is
if (myObj == null)
{
Type myObjType = myObj.GetType();

This will be evaluated at runtime and will return the correct type.

Hi Ignacio,
Type myObjType = myObj.GetType();

You can't do that, because myObj is null. You'll get a
NullReferenceException.
 
B

Barry Kelly

void EnsureObjectExists(ref MyObj1 myObj)
MyObj1 obj1 = null;
MyObj2 obj2 = null;
EnsureObjectExists(obj1);
EnsureObjectExists(obj2);

You know these need to be "EnsureObjectExists(ref obj1)", right?
Do you guys have an idea how to do something like this? Maybe it isn't
possible at all but hopefully it is ;-)

With generics, yes:

---8<---
static void Create<T>(ref T value)
where T : class
{
// typeof(T) is the System.Type
if (value == null)
// choose your constructor and assign
}
--->8---

Alternatively, if you only need the default constructor (no parameters):

---8<---
static void Create<T>(ref T value)
where T : class, new()
{
if (value == null)
value = new T();
}
--->8---

-- Barry
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
You can't do that, because myObj is null. You'll get a
NullReferenceException.


Ooops, my mistake :)

I need coffee I did not see the == , I assume it was != (which would have
no sense at all in the first place )

Going to prepare myself a hot strong cuban coffee :)
 
M

marc

hey thanks all of you.
the one with the generic seems to be what i am looking. my luck i am
using 2.0 :)))

cheers, marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top