Possible to use generic type in one method for other class methods?

S

Steve K.

I know this code is garbage but it should give you an idea of what I'm
after:
<code>
class Test
{
Type _t = null;
public void SetSourceList<T>(Dictionary<string, T> sourceList)
{
_t = typeof(T);
}

public _t DoSomething()
{
return default(_t);
}
}

</code>

Basically I've got two related methods of a class that I want to be generic
and I don't want to make the whole class generic.

Anyway to accomplish what I'm after? I've been reading:
http://msdn2.microsoft.com/en-us/library/system.type.makegenerictype(vs.80).aspx

But it's not sinking in too well, maybe I need to get some rest ;0)

-Steve
 
J

Jon Skeet [C# MVP]

I know this code is garbage but it should give you an idea of what I'm
after

<snip>

It's not entirely clear to me, to be honest. It looks like you're
trying to make it "half-generic" which doesn't tend to work terribly
well in my experience.

In particular, generics occur at *compile-time* as well as runtime.
With your example, how would you expect the compiler to validate
assignments based on the results of a call to DoSomething?

You might consider deriving a generic type from the non-generic one,
or vice versa.

Jon
 
L

Lasse Vågsæther Karlsen

Steve said:
I know this code is garbage but it should give you an idea of what I'm
after:
<code>
class Test
{
Type _t = null;
public void SetSourceList<T>(Dictionary<string, T> sourceList)
{
_t = typeof(T);
}

public _t DoSomething()
{
return default(_t);
}
}

</code>

Basically I've got two related methods of a class that I want to be generic
and I don't want to make the whole class generic.

Anyway to accomplish what I'm after? I've been reading:
http://msdn2.microsoft.com/en-us/library/system.type.makegenerictype(vs.80).aspx

But it's not sinking in too well, maybe I need to get some rest ;0)

-Steve

You can use Activator to create an instance of the _t type object, but
you need to return it as System.Object
 
I

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

Hi,


Why are you trying to do that?

What is wrong in doing the entire class generic.
 
S

Steve K.

Hi All,

Apologies if my 2AM post wasn't clear or didn't make sense. I should learn
to not send questions at the tail end of a long work day ;) (On a side
note, have you heard about the phone service that will let you specify
certain numbers to make unavailable after a certain hour? The idea is to
prevent drunken calls to ex boyfriends/girfriends. Funny)

Anyway, the reason for my question can be understood here (2nd bullet under
"What I need to do":
http://pmddirect.com/sklett/PMDTextBoxReview.html

I'd like to avoid making my control class a generic and instead find some
way to pass in data via a generic method and get it back out on the same
type-safe manner.

I hope that clears up any confusion. Thanks for the replies and again I'm
sorry if the question didn't makse sense.

-Steve
 
B

Ben Voigt [C++ MVP]

Steve K. said:
Hi All,

Apologies if my 2AM post wasn't clear or didn't make sense. I should
learn to not send questions at the tail end of a long work day ;) (On a
side note, have you heard about the phone service that will let you
specify certain numbers to make unavailable after a certain hour? The
idea is to prevent drunken calls to ex boyfriends/girfriends. Funny)

Anyway, the reason for my question can be understood here (2nd bullet
under "What I need to do":
http://pmddirect.com/sklett/PMDTextBoxReview.html

I'd like to avoid making my control class a generic and instead find some
way to pass in data via a generic method and get it back out on the same
type-safe manner.

Ah hah. The "right" way to solve your problem is to make the whole class
generic, but then you run into the lack of support for generics in the
designer environment. Not sure what can be done about that.
 

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