471,325 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Cast generic IFoo<T> : last level (I hope)

Hello again,

My last question which opened a big debate about casting such a generic
interface learned me a lot, but I can't find a way because of my particular
generic interface.
I will shown you my code, but when you will answer my post, don't write:
- he is stupid (I already know)
- this is not Object Oriented
- generic is not made for that ...

abstract class BaseStaticOverride<Twhere T : BaseStaticOverride<T>
{

private static T _instance;

private static object _lock = new object();

public static T Instance
{

get
{

if (_instance == null)
{

lock (_lock)
{

if (_instance == null)

_instance =
(T)Activator.CreateInstance(typeof(T), true);

}

}

return _instance;

}

}
}

abstract class C1Base<T: BaseStaticOverride<Twhere T : C1Base<T>
{

protected C1Base()
{

}

public void WriteLine()
{

Console.WriteLine(Line);

}
protected abstract string Line
{

get;

}
}
class C1 : C1Base<C1>
{

protected C1()
{

}

protected override string Line
{

get { return "C1"; }

}

}

C1 can be considered as a static method with my code and so C1 and
C1Base<C1are the same class.
The problem occures when I don't know which class implement C1Base
(C1Base<?>). Since C1Base<?can be considered as static, I think it would be
possible to do this:
C1Base<?>.Instance.WriteLine(); // which will return "C1"

Hope you understand what I want to do.

Thanks in advance for your help.
Jul 25 '08 #1
4 1108
On Jul 25, 1:29*pm, Alphapage <Alphap...@discussions.microsoft.com>
wrote:

<snip>
Hope you understand what I want to do.
Not really, I'm afraid. You're referring to C1 as a static *method*
but it's a *class*.

On a somewhat different note, your use of double-checked locking isn't
threadsafe. If you make _instance volatile it would be safe, but
generally I'd just use a static initializer instead. See
http://pobox.com/~skeet/csharp/singleton.html

Jon
Jul 25 '08 #2
Your reference to Java only served to confuse me further :-)

What (in .NET) is wrong with his double-lock approach?

Jul 25 '08 #3
On Jul 25, 1:50*pm, "Peter Morris" <mrpmorri...@SPAMgmail.comwrote:
Your reference to Java only served to confuse me further :-)

What (in .NET) is wrong with his double-lock approach?
Without the "volatile" flag there's still the possibility that a
client will see a non-null reference before all of the writes have
been flushed, I believe. In other words, it could see a half-
constructed object.

Lock-free programming is always tricky, and almost never a good idea
for mere mortals like ourselves. I'll leave it to Joe Duffy, who knows
what he's talking about. (Even he's worried at the moment, as one
memory model isn't quite what he thought it was...)

Using a static initializer is simpler, safer, and still lazy (if you
include the static constructor).

Jon
Jul 25 '08 #4
On Fri, 25 Jul 2008 05:29:01 -0700, Alphapage
<Al*******@discussions.microsoft.comwrote:
Hello again,

My last question which opened a big debate about casting such a generic
interface learned me a lot, but I can't find a way because of my
particular
generic interface.
I will shown you my code, but when you will answer my post, don't write:
- he is stupid (I already know)
- this is not Object Oriented
- generic is not made for that ...
Okay, I won't. :) Especially the first...I try very hard to never write
things like that even when I know them to be true, and there's nothing
here to suggest it is.

On the other two points, I guess it just remains to be seen. As Jon says,
I don't think this code example clears things up much, if at all. In
addition to his comment, I'll point out that I've never seen correct code
where the type parameter is constrained to inherit the type in which it's
being used.

It's possible that there's some esoteric use of generics that I'm
unfamiliar with where it comes up, but the few times I've played around
with that construct myself, I've gotten nowhere and coded myself into
logical impossibilities. If you're sure that that's the right tack to
take, you need to at least elaborate on _why_ you're declaring your
generic classes that way.

Pete
Jul 25 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jasper Kent | last post: by
4 posts views Thread by Sameh Ahmed | last post: by
3 posts views Thread by David Veeneman | last post: by
7 posts views Thread by Andrus | last post: by
6 posts views Thread by Andrus | last post: by
15 posts views Thread by Lloyd Dupont | last post: by
reply views Thread by rosydwin | last post: by

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.