[Mono-list] Implementing mix-ins...

Jonathan Pryor jonpryor@vt.edu
17 Dec 2002 18:54:33 -0500


Attributes are used to associate data with particular elements, but no
"insertion" is performed.

The problem (as far as your request is concerned) is how Attributes are
used.  You can query a type/member/etc. for its Attributes at runtime
(possibly changing behavior as you do so), but the attributes that are
available on an object are not available at compile time.

It would be akin to wanting to know the result of an arbitrary function
call at compile time, but the function can't be executed (and the result
known) until run-time.

In short, Attributes won't work as you would like them to.

At present, there isn't any real way (AFAIK) to provide mix-ins in C#.

That will change in the future, when Generics are provided.  I believe
you can inherit from a template argument, which would allow a form of
mix-ins to be written as template classes, which can be inherited from
(to an arbitrary depth) to provide services.  For example:

	// Mixin class
	class Foo <Base> : Base {
		public string A { /* ... */ }
	}

	// Use the mixin
	class Bar : Foo <Object> {
		public void Test () {Console.WriteLine (A);}
	}

	// Another Mixin
	class Baz <Base> : Base {
		public object B { /* ... */ }
	}

	// Nest the mix-ins, so Qux mixes Foo & Baz.
	class Qux : Baz<Foo<Object> > {
		public void Test () {
			Console.WriteLine (A);
			Console.WriteLine (B);
		}
	}

Generics will provide most of what's needed to "properly" do mix-ins.

 - Jon

On Tue, 2002-12-17 at 17:51, Freeman Pascal wrote:
> I suspect this is way off topic.  If someone could 
> point me to a better forum I would appreciate it.
> 
> I'm interested in implementing true mix-ins in C#.
> I know this can be done using interfaces, but 
> interfaces force the author to provide an 
> implementation for the interface within the class.
> This forces the implementation to be specific to 
> the class implementing the interface and does not
> allow a generic solution.
> 
> A broader solution would be to write the class to
> be derived from an abstract class that provides
> a general solution to implementing the interface,
> but with a single inheritance model this can be
> problematic at times.
> 
> Is it possible to use attributes in C#/VB.NET to
> have code inserted or associated at compile time
> with a class in such a way as it appears to have
> been originally written within the context of the 
> using class?
> 
> module FooBarBaz
> {
>     public class Foo
>     {
>         public String A
>         {
>             get { return _a; }
>             set { _a = value; }
>         }
> 
>         private string _a = "Bilbo";
>     }
> 
>     [MixIn (Foo)]
>     public class Bar
>     {
>         public void test ()
>         {
>             Console.Out.WriteLine (A);	// prints "Bilbo"
>         }
>     }
> 
>     [MixIn (Foo)]
>     public class Baz
>     {
> 	  public Baz ()
>         {
>             A = "Fordo";
>         }
> 
>         public void test ()
>         {
>             Console.Out.WriteLine (A);	// prints "Frodo"
>         }
>     }
> }
> 
> I hope I have explained enough to get my idea across.
> 
> -Freeman
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list