Re: Do we have extention properties in C# 3.0?
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Thu, 24 Jul 2008 19:09:38 -0700
On Thu, 24 Jul 2008 17:10:38 -0700, not_a_commie <notacommie@xxxxxxxxx> wrote:
My biggest complaint with the extension methods is that they don't
support the 'where' clause. You have to specify the exact type. I'd
rather see support for that before adding support for properties.
Do you mean something like this:
using System;
namespace TestGenericExtensionMethod
{
static class Extensions
{
public static void TestMethod<T>(this T t) where T : TestBase
{
Console.WriteLine("{0}.TestMethod", t.GetType().Name);
}
}
class TestBase
{
}
class TestDerived : TestBase
{
}
class Program
{
static void Main(string[] args)
{
TestDerived td = new TestDerived();
Object obj = new Object();
td.TestMethod(); // works fine
// obj.TestMethod(); // CS0311 error
Console.ReadLine();
}
}
}
Your wish is granted! :)
I'd
also like to see the 'where' clause enhanced to support 'primitive',
meaning types that support these operators: +,-,/,*,%, etc. or maybe
some way to specify operators in the 'where' clause.
I think lots of people would like to see useful ways to use generics with operators like that, whether via a constraint or some other mechanism. Marc Gravell has posted a useful workaround to the current situation. You should be able to use Google Groups to look back at the archives and find his articles on the topic.
Maybe that will also appear in a future C#. It's not a trivial problem to solve (extension properties would be much easier :) ).
Pete
.
- Follow-Ups:
- Re: Do we have extention properties in C# 3.0?
- From: not_a_commie
- Re: Do we have extention properties in C# 3.0?
- From: Marc Gravell
- Re: Do we have extention properties in C# 3.0?
- References:
- Do we have extention properties in C# 3.0?
- From: Max2006
- Re: Do we have extention properties in C# 3.0?
- From: Arne Vajhøj
- Re: Do we have extention properties in C# 3.0?
- From: Peter Duniho
- Re: Do we have extention properties in C# 3.0?
- From: Arne Vajhøj
- Re: Do we have extention properties in C# 3.0?
- From: Peter Duniho
- Re: Do we have extention properties in C# 3.0?
- From: Frans Bouma [C# MVP]
- Re: Do we have extention properties in C# 3.0?
- From: not_a_commie
- Do we have extention properties in C# 3.0?
- Prev by Date: What cryptoalgorithm supports CipherMode.CTS?
- Next by Date: Downlevel .NET question
- Previous by thread: Re: Do we have extention properties in C# 3.0?
- Next by thread: Re: Do we have extention properties in C# 3.0?
- Index(es):
Relevant Pages
|