Reflections in C#

Back when I first read about reflections I was like who needs that ?! Don’t we always know what is the sequence of instructions to do ?! And once new changes come, only need to write a bit more code to mirror it.
Yeah, I know what your saying “learn how to code”, well I learned that the hard way.
After many modifications and adding code things got pretty much out of hand which lead to more time for developing, testing and lunching any new feature. :.( …
So where do reflections fit in? Well, in one of project at my current company we have a central Video On Demand (VOD) server which consumes from many sources and feeds a lot of services.
Due to this nature, the sources that provide us with videos could be of many types (Partner Hosted, Us and Partner side, Us only) and each has there own logic to fetch and work with the data.
Someone would say there is no need! For that just tag the content to which provider and then using the interfaces and some abstract classes will get the job done!.
Yes it would!
But as more and more sources get added, there is going to be more checks and that is a developer nightmare.
Any issue would mean that you have to go through all the checks to actually start identifying what is happening plus each time we want to add leads to a modification in the core code base to adjust to the changes.
That was a mouth full. Enough taking lets start coding ! :).
So in basic words reflections helps us to load classes, create object and pretty much anything during RUN TIME!, yeah you read correct run time.
So we can tie our classes, methods or properties with some attributes and we are good to go.
First lets create our know parts, Interfaces and attributes.
public interface IContentProvider
{
string WhoAmI();
}
/// <summary>
/// An arrtribute to Identify which implementation to use
/// </summary>
public class ContentProviderAttribute : Attribute
{
#region Properties
/// <summary>
/// Some unique Id or you can add as many as you want
/// </summary>
public int Id { get; set; }
#endregion
public ContentProviderAttribute(int id)
{
Id = id;
}
}
Now we are ready to create our implementations, for me I created new project so I separate them from the core logic.
[ContentProvider(1)]
public class ProviderOne : IContentProvider
{
public string WhoAmI()
{
return nameof(ProviderOne);
}
}
[ContentProvider(2)]
public class ProviderTwo : IContentProvider
{
public string WhoAmI()
{
return nameof(ProviderTwo);
}
}
Done!, we are ready to test and see how the magic works. I created a simple Console App to demonstrate it.
static void Main(string[] args)
{
//Load the assembly based on its name
// NOTE: you need to make sure that your .DLL are in the right folder path else
// provide as a full path in the asembly name.
// If you want just copy the .dll in the debug file
Assembly assembly = Assembly.LoadFrom("ReflectionsTestProviders.dll");
var providers = assembly.GetTypes()
.Where(item => item.GetCustomAttribute(typeof(ContentProviderAttribute)) != null);
foreach (var item in providers)
{
try
{
var provider = (IContentProvider)Activator.CreateInstance(item);
Console.WriteLine($"Provider {provider.WhoAmI()}");
}
catch (Exception ex)
{
Console.WriteLine($"Error while creating providers ex {ex.Message}");
}
}
Console.ReadLine();
}
After running the code each of the implementation will do his own logic and the best thing I can add more and just update the content provider DLL with out worrying about the core code. isn’t that awosome.
1 Comment
it was useful information thanks for your advice, keep going…