Registration of derived classes with reflection, good or evil?

As we all know, when we derive a class and use polymorphism, someone must know somewhere which class should implement it. We can use factories, a large switch statement, if-else-if, etc. I just learned from Bill K that this is called Injection Dependency.

My question is: is reflection and attributes good as a dependency injection mechanism? Thus, the list is populated dynamically as new types are added.

Here is an example . Please do not report how you can upload images in other ways, as we know.

Suppose we have the following ImageFileFormat interface:

public interface IImageFileFormat
{
   string[] SupportedFormats { get; };
   Image Load(string fileName);
   void Save(Image image, string fileName);
}

Various classes implement this interface:

[FileFormat]
public class BmpFileFormat : IImageFileFormat { ... }

[FileFormat]
public class JpegFileFormat : IImageFileFormat { ... }

, Load()/Save() Extensions.

class ImageLoader
{
   public Image Load(string fileName)
   {
      return FindFormat(fileName).Load(fileName);
   }

   public void Save(Image image, string fileName)
   {
      FindFormat(fileName).Save(image, fileName);
   }

   IImageFileFormat FindFormat(string fileName)
   {
      string extension = Path.GetExtension(fileName);
      return formats.First(f => f.SupportedExtensions.Contains(extension));
   }

   private List<IImageFileFormat> formats;
}

, , () .

:

public ImageLoader()
{
   formats = new List<IImageFileFormat>();
   formats.Add(new BmpFileFormat());
   formats.Add(new JpegFileFormat());
}

:

public ImageLoader()
{
   formats = new List<IImageFileFormat>();
   foreach(Type type in Assembly.GetExecutingAssembly().GetTypes())
   {
      if(type.GetCustomAttributes(typeof(FileFormatAttribute), false).Length > 0)
      {
         formats.Add(Activator.CreateInstance(type))
      }
   }
}

, , . , , , , , , , .

, .

+3
5

, - - , - , IMHO. , , .

, - , - // ( DLL).

Coincoin:

, .

  • , MSBuild <UseTask>
  • , , Microsoft Visual Studio.
  • .
+4

, . - , . , ImageLoader,

+3

, Injection Dependency?

, , , .

, DI , <interface> , DI , .

IoC (Inversion of Control), - , ( DI) .

+1

, " ", , ImageLoader ImageFileFormats, ImageFileFot ImageLoader? :

  • , IImageFileFormat, - ( )
  • ImageLoader, , IImageFileFormat.
0

In vb.net, if all image downloaders are in the same assembly, partial classes and events can be used to achieve the desired effect (to have a class whose purpose is to trigger an event when image downloaders are to be registered, each file containing image downloaders, can use a "partial class" to add another event handler to this class); C # does not have a direct equivalent to vb.net WithEvents syntax, but I suspect partial classes are a limited mechanism to achieve the same.

0
source

All Articles