Skip to main content
added 853 characters in body
Source Link
Jon Skeet
  • 1.5m
  • 893
  • 9.3k
  • 9.3k

It's not possible like this, no - because your list is a list of types, not references to instances of that type.

The closest you can easily come, assuming that there's a parameterless constructor for each type, is:

foreach (Type handlerType in handlers) { // Create an instance of the handler type TokenBaseClass handler = (TokenBaseClass) Activator.CreateInstance(handlerType); if (handler.HandlesToken(AToken)) { instantiatehandler.HandleToken(AToken); break; } } 

EDIT: In answer to your question in the comments, I would handle this slightly differently.

I would change your List<Type> to a List<Func<TokenKind, TokenClassBase>>. In other words, a list of factory functions, from TokenKind to TokenClassBase. The function for each type would depend on the type, but it would either return an instance of TokenClassBase, or null if that TokenKind couldn't be handled.

Then you'd use:

foreach (var handlerFactory in handlerFactories) { TokenBaseClass handler = handlerFactory(AToken); if (handler != null)  {  handler.HandleToken(AToken);   break;   } } 

The way that you'd create the delegates would depend on the exact nature of your code, but you could either use a lambda expression or a method group conversion, probably from a static method.

It's not possible like this, no - because your list is a list of types, not references to instances of that type.

The closest you can easily come, assuming that there's a parameterless constructor for each type, is:

foreach (Type handlerType in handlers) { // Create an instance of the handler type TokenBaseClass handler = (TokenBaseClass) Activator.CreateInstance(handlerType); if (handler.HandlesToken(AToken)) { instantiate(handler); handler.HandleToken(AToken); break; } } 

It's not possible like this, no - because your list is a list of types, not references to instances of that type.

The closest you can easily come, assuming that there's a parameterless constructor for each type, is:

foreach (Type handlerType in handlers) { // Create an instance of the handler type TokenBaseClass handler = (TokenBaseClass) Activator.CreateInstance(handlerType); if (handler.HandlesToken(AToken)) { handler.HandleToken(AToken); break; } } 

EDIT: In answer to your question in the comments, I would handle this slightly differently.

I would change your List<Type> to a List<Func<TokenKind, TokenClassBase>>. In other words, a list of factory functions, from TokenKind to TokenClassBase. The function for each type would depend on the type, but it would either return an instance of TokenClassBase, or null if that TokenKind couldn't be handled.

Then you'd use:

foreach (var handlerFactory in handlerFactories) { TokenBaseClass handler = handlerFactory(AToken); if (handler != null)  {  handler.HandleToken(AToken);   break;   } } 

The way that you'd create the delegates would depend on the exact nature of your code, but you could either use a lambda expression or a method group conversion, probably from a static method.

Source Link
Jon Skeet
  • 1.5m
  • 893
  • 9.3k
  • 9.3k

It's not possible like this, no - because your list is a list of types, not references to instances of that type.

The closest you can easily come, assuming that there's a parameterless constructor for each type, is:

foreach (Type handlerType in handlers) { // Create an instance of the handler type TokenBaseClass handler = (TokenBaseClass) Activator.CreateInstance(handlerType); if (handler.HandlesToken(AToken)) { instantiate(handler); handler.HandleToken(AToken); break; } }