I think I'm really confused about what I can do with AutoFac, can someone please put me in the way.
I have a base type
class PersonBase{
public string SaySomething(){
return "I am base";
}
}
I get two specific classes
class FakePerson : PersonBase{
public override string SaySomething(){
return "I'm so Fake";
}
}
class RealPerson : PersonBase{
public override string SaySomething(){
return "I am For Real";
}
}
Create a generic PersonHandler class to deal with different types of people and would like PersonHandler to instantiate the person at the appropriate time, so I don’t want the Person instance to be introduced, just need to get the derived type
class PersonHandler<T>
where T : PersonBase, new() {
T _Person;
public DoWork(){
_Person = new T();
_Person.SaySomething();
}
}
Now I am trying to use a handler after registering types, as described below, with different results.
var ph = contrainer.Resolve<PersonHandler<PersonBase>>();
ph.DoWork();
I tried registering types as follows
1. vBuilder.RegisterType<PersonHandler<FakePerson>>().As<PersonHandler<PersonBase>>();
This gives me an error indicating that it is PersonHandler<FakePerson>not assigned PersonHandler<PersonBase>(or vice versa, I do not repeat which one)
2. vBuilder.RegisterGeneric<typeof(PersonHandler<>)>
vBuilder.RegisterType<FakePerson>().As<PersonBase>();
PersonBase FakePerson, PersonHandler<PersonBase>, " "
3. vBuilder.RegisterGeneric(typeof(PersonHandler<FakePerson>)).As(typeof(PersonHandler<PersonBase>));
, , PersonHandler<FakePerson>
, , , ,
,