问题描述:

Just found LinFu - looks very impressive, but I can't quite see how to do what I want to do - which is multiple inheritance by mixin (composition/delegation as I'd say in my VB5/6 days - when I had a tool to generate the tedious repetitive delegation code - it was whilst looking for a C# equivalent that I found LinFu).

FURTHER EDIT: TO clarify what I mean by composition/delegation and mixin.

public class Person : NEOtherBase, IName, IAge

{

public Person()

{

}

public Person(string name, int age)

{

Name = name;

Age = age;

}

//Name "Mixin" - you'd need this code in any object that wanted to

//use the NameObject to implement IName

private NameObject _nameObj = new NameObject();

public string Name

{

get { return _nameObj.Name; }

set { _nameObj.Name = value; }

}

//--------------------

//Age "Mixin" you'd need this code in any object that wanted to

//use the AgeObject to implement IAge

private AgeObject _ageObj = new AgeObject();

public int Age

{

get { return _ageObj.Age; }

set { _ageObj.Age = value; }

}

//------------------

}

public interface IName

{

string Name { get; set; }

}

public class NameObject : IName

{

public NameObject()

{}

public NameObject(string name)

{

_name = name;

}

private string _name;

public string Name { get { return _name; } set { _name = value; } }

}

public interface IAge

{

int Age { get; set; }

}

public class AgeObject : IAge

{

public AgeObject()

{}

public AgeObject(int age)

{

_age = age;

}

private int _age;

public int Age { get { return _age; } set { _age = value; } }

}

Imagine objects with many more properties, used in many more "subclasses" and you start to see the tedium. A code-gernation tool would actually be just fine...

So, LinFu....

The mixin example below is fine but I'd want to have an actual Person class (as above) - what's the LinFu-esque way of doing that? Or have I missed the whole point?

EDIT: I need to be able to do this with classes that are already subclassed.

DynamicObject dynamic = new DynamicObject();

IPerson person = null;

// This will return false

bool isPerson = dynamic.LooksLike<IPerson>();

// Implement IPerson

dynamic.MixWith(new HasAge(18));

dynamic.MixWith(new Nameable("Me"));

// Now that it’s implemented, this

// will be true

isPerson = dynamic.LooksLike<IPerson>();

if (isPerson)

person = dynamic.CreateDuck<IPerson>();

// This will return “Me”

string name = person.Name;

// This will return ‘18’

int age = person.Age;

相关阅读:
Top