LinFuを見つけたばかりです-非常に印象的ですが、私がやりたいことを行う方法がよくわかりません-これは、ミックスインによる多重継承です(VB5/6日間で言うように構成/委任-面倒な反復委任コードを生成するためのツール-私がLinFuを見つけたのと同等のC#を探していたとき)。
さらに編集:構成/委任とミックスインの意味を明確にするため。
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; } }
}
より多くの「サブクラス」で使用される、より多くのプロパティを持つオブジェクトを想像してみてください。退屈な作業が始まります。コード生成ツールは実際には問題ありません...
したがって、LinFu ....以下のミックスインの例は問題ありませんが、実際のPersonクラス(上記のように)が必要です-LinFu風の方法は何ですか?それとも私は全体のポイントを逃しましたか?
編集:私はすでにサブクラス化されているクラスでこれを行うことができる必要があります。
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;