I'm not sure I understand exactly what you're trying to do so this code may not be helpful but I'll take a stab at it.
First, if your generic type constraint ensures that type T has a parameterless constructor you should use new T() instead of Activator.CreateInstance().
Secondly, it seems like you're trying to instantiate type T and then return an property of that type. That seems like an odd use-case to me as the instance of T is then essentially discarded, but assuming that is what you're wanting to do you could do so like this:
public partial class Factory<T> where T : new()
{
public static U Get<U>(Func<T, U> selector)
{
var instance = new T();
return selector(instance);
}
}
Factory<Member>.Get(m => m.FirstName);
Again, I'm not sure that is what you're asking for but it seems like it is. You could go further if you want to ensure a specific subclass or interface type on the Get method with a generic type constraint on type U. For instance where U : IWorker.
This approach gives you a single method with intellisense for selecting the members of the factory type, but as I've said it doesn't seem very useful in its current state.
------------------ Update based on closer review of the question -----------------
I think that the following code might be more what you're looking for. If it is I'd suggest that you consider, based on what you're using it for, either switching to a Dependency Injection / Inversion of control framework like Autofac or StructureMap if you're trying to instantiate and return non-data objects or to a data framework like EntityFramework or NHibernate if you are trying to return data from a datastore on demand.
Nevertheless... here is my code which I've not actually checked to see if it will compile:
public interface IFactory
{
object Get(int entityId);
}
public interface IFactory<T> : IFactory
{
T Get(int entityId);
}
public abstract class Factory<T> : IFactory<T>
{
abstract T Get(int entityId);
object IFactory.Get(int entityId) {
return Get(entityId);
}
}
public class MemberFactory : Factory<Member>
{
public override Member Get(int entityId) {
return new Member { MemberId = entityId, FirstName = "John", LastName = "Doe" };
}
}
public class GenericFactory<T> : Factory<T>
{
private Func<int, T> constructor;
public GenericFactory(Func<int, T> constructor)
{
this.constructor = constructor;
}
public override T Get(int entityId) {
return constructor(entityId);
}
}
public static class Factory
{
private static ConcurrentDictionary<Type, IFactory> TypeFactories = new ConcurrentDictionary<Type, IFactory>();
public static void RegisterFactory<T>(IFactory<T> factory)
{
TypeFactories.AddOrUpdate(typeof(T), t => factory, (t, f) => factory);
}
public static T Get<T>(int entityId)
{
var type = typeof(T);
IFactory untypedFactory
if (!TypeFactories.TryGetValue(type, out untypedFactory))
{
throw new FactoryNotFoundException(type);
}
var factory = untypedFactory as IFactory<T>;
if (factory == null)
{
throw new StopMessingWithMyPrivateStaticDictionaryException();
}
return factory.Get(entityId);
}
}
Factory.RegisterFactory(new MemberFactory());
Factory.RegisterFactory(new GenericFactory<Product>(id => new Product { ProductId = id }));
Factory.RegisterFactory(new GenericFactory<FooBar>(id => new FooBar(id));
var member = Factory.Get<Member>(1234);