generic instantiation

T

tbh

I'd like to create a generic method that returns a list of objects derived
from a class that "wraps" a guid.
public static List<ID> GetList<ID>(...) where ID : MyGuidWrapper {
...
}

however in the step(s) where I create one of my ID objects:

// this works, I guess because MyGuidWrapper (a real class) knows how to
instantiate itself from a system Guid
MyGuidWrapper ing = new MyGuidWrapper(dr.GetGuid(0));

// but this yields a run-time error (can't cast.)
ID id = (ID)ing;

// and this runs without error but afterwards id is null
ID id = ing as ID;


Am I trying to do something that is impossible in C# or have I just not
figured out how to spell it yet?


I also wonder if I can specify a constraint on the order of:
Where ID:
- can be converted to and from a guid, e.g.
- hast a method Guid ToGuid() and
- is "newable" from a Guid; quasi: new(Guid)

under "constraints" I've found a new constraint, but only a parameterless
one. and I haven't figured out how to spell this as an interface yet either.


Cheers,

Tim Hanson
 
M

Marc Gravell

I'm a little unclear on what you'd *like* to code, but perhaps one
approach (akin to much of LINQ) would be to pass a converter delegate
into the method - i.e. a Converter<MyGuidWrapper, ID>? Or have a
suitable method on the known base-class?
Some casts (with generics) can be done by putting (object) in the
middle - i.e. Something s = (Something)(object)value;
but this isn't very tidy.
I also have some soon-to-be-released generic utility methods that
includes a more robust conversion method; would this be of interest?
(most of it is aimed at providing maths for generics; this just came
for free...)

Marc
 
T

tbh

Marc Gravell said:
I'm a little unclear on what you'd *like* to code, but perhaps one
approach (akin to much of LINQ) would be to pass a converter delegate
into the method - i.e. a Converter<MyGuidWrapper, ID>? Or have a
suitable method on the known base-class?

thanks, Marc, I'll see if I can get something like that to work.

[i think i somewhat glossed over the meaning of the "as" operator. i'm
reading it more carefully now to see if what I want is possible without a
converter. hmmm, seems to be in theory. since my particular ID contains a
constructor from a MyGuidWrapper.]
Some casts (with generics) can be done by putting (object) in the
middle - i.e. Something s = (Something)(object)value;
but this isn't very tidy.
I also have some soon-to-be-released generic utility methods that
includes a more robust conversion method; would this be of interest?
(most of it is aimed at providing maths for generics; this just came
for free...)

good to know, thanks. where will these be released?

cheers,

Tim
 
T

tbh

hi, Marc,

thanks again for the suggestion. i got the delegate idea to work, though it
seems somewhat kludgy to me (and by my reading of the C# 2.0 book "as"
should have worked).

this reminds me: i forgot to mention I am using DotNet 2.0. maybe that
matters? we'll be foraying into 3 or 3.5 soon, but not just yet.

thanks again.

Tim
 
B

Ben Voigt [C++ MVP]

tbh said:
hi, Marc,

thanks again for the suggestion. i got the delegate idea to work, though
it seems somewhat kludgy to me (and by my reading of the C# 2.0 book "as"
should have worked).

"as" is a runtime type check (downcast), never a conversion.
this reminds me: i forgot to mention I am using DotNet 2.0. maybe that
matters? we'll be foraying into 3 or 3.5 soon, but not just yet.

The LINQ suggestions won't work without C# 3.
 
M

Marc Gravell

Ben Boigt> The LINQ suggestions won't work without C# 3.
Well, you can't use a lambda without C# 3... but if you mean (as I
did) the functional approach of using delegates a lot more than
perhaps we have historically, then that will work just fine in 2.0.

tbh> good to know, thanks. where will these be released?
Hopefully as part of Jon's MiscUtil library: http://tinyurl.com/2euejk
However, the bad news is that this relies heavily on some .NET 3.5
functionality, and won't work (without a huge amount of effort)
without it. Sorry... the truth is I simply found a lazy way of doing
some very flexible things with generics, but the "lazy" comes from
3.5.

Marc
 
T

tbh

thanks, Ben:
"as" is a runtime type check (downcast), never a conversion.

it would be great to understand this precisely.

in Hejlsberg et al's C# quasi-bible, "as" is explained in 20.8.6 and 7.9.10
and refers to implicit and explicit conversions (6.2.3), which states (2nd
case of many): "from any class-type S to any class-type T, provided S is a
base class of T", which is the case in my problem.
The LINQ suggestions won't work without C# 3.

i think Marc meant LINQ-like approaches and as I mentioned the delegate
approach worked fine, i just found it a bit more awkward than expected.

but thanks to both of you. my motivation to try out DotNet 3 and 3.5
increaseth. :)

cheers,

Tim
 
M

Marc Gravell

as I mentioned the delegate approach worked fine, i just found it
a bit more awkward than expected.

Note that C# 3 can make this a lot less awkward via lambdas - and note
that you can use C# 3 with .NET 2.0 (give or take a service-pack ;-p)

Marc
 
T

tbh

Note that C# 3 can make this a lot less awkward via lambdas - and note
that you can use C# 3 with .NET 2.0 (give or take a service-pack ;-p)

good to know, thanks.

[upgrades in an environment with many www Servers can be somewhat painful,
so sometimes we elect to leap-frog one level. (we might go from sql2000
directly to 2008, for example; might go straight from DotNet 2 to 3.5 as
well).]
 
M

Marc Gravell

I know exactly what you mean. But that isn't necessarily a barrier to
using Orcas and C# 3 - as long as you target .NET 2.0 (Orcas is
multi-targetting).

Most of the C# 3 language features (including lambdas as delegates)
are fully usable with .NET 2.0; some (extension methods) are usable
but require a minor cludge (declaring an ExtensionAttribute; 3 lines
of code...); the LINQ stuff (i.e. an implementation of LINQ-to-object)
/can/ be made to work, but to be honest it probably isn't worth it
unless you *really, really* need it in 2.0; better to wait to upgrade
to .NET 3.5.

Daniel Moth has some good posts here:
http://www.danielmoth.com/Blog/2007/05/using-c-30-from-net-20.html

Marc
 
B

Ben Voigt [C++ MVP]

tbh said:
thanks, Ben:


it would be great to understand this precisely.

in Hejlsberg et al's C# quasi-bible, "as" is explained in 20.8.6 and
7.9.10 and refers to implicit and explicit conversions (6.2.3), which
states (2nd case of many): "from any class-type S to any class-type T,
provided S is a base class of T", which is the case in my problem.

"as" checks if the true runtime type (most-derived, returned by
System.Object.GetType() method of the object) is derived from (or
implements, in the case of an interface) the type requested. It never
creates a new object.
 
J

Jon Skeet [C# MVP]

"as" checks if the true runtime type (most-derived, returned by
System.Object.GetType() method of the object) is derived from (or
implements, in the case of an interface) the type requested. It never
creates a new object.

<obPedant>
It can via boxing:

class Test
{
static void Main()
{
int i = 5;

IComparable o = i as IComparable;
Console.WriteLine(o);
}

}
</obPedant>
 
B

Ben Voigt [C++ MVP]

Jon Skeet said:
<obPedant>
It can via boxing:

class Test
{
static void Main()
{
int i = 5;

IComparable o = i as IComparable;
Console.WriteLine(o);
}

}
</obPedant>

Ok, but that's a copy of the existing object, type and all. It still won't
create an object with a new type.
 

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