Blog: Reflection

C# 8: The output of Nullable Reference Types and how to reflect it

September 28, 2018, (updated on October 15, 2018), 2 comments, Software Development

After reading some articles about the new C# 8 feature Nullable Reference Types, one question still was unanswered: How does a compiled assembly with nullable types enabled look like and how can I access the nullability information via reflection? Because I couldn’t find an article with an answer, I had to figure it out myself. First I wrote a simple class which I wanted to analyze: [module: System.Runtime.CompilerServices.NonNullTypes(true)] class Test { public string? Foo {get; set;} public void Bar(string? baz, string buz) { } } With the help of sharplab.io I compiled it with C# 8 and looked at the […] Read more...

Tags: , , ,

Angular: Convert input values to the correct type with a TypeScript property decorator

February 24, 2016, (updated on December 19, 2017), 13 comments, Software Development

I recently played with Angular and found an uncomfortable problem: Declaring an input property with a TypeScript type does not guarantee that the input value will always be of this type. This is because the Angular framework may update an input with a wrongly typed value and thus is able to “circumvent” the TypeScript type system. As a result, you may end up with strange and unexpected runtime errors. Let’s have a look at this simple Angular component: @Component({ selector: "my-component" }) export class MyComponent { @Input() enabled = true; showValue() { alert(typeof this.enabled); } } As shown in the […] Read more...

Tags: , , , ,

Avoiding DLL file locks when using .NET reflection in external assemblies

September 6, 2015, (updated on January 7, 2016), 1 comment, Software Development

For one of my projects, I had to load external assemblies and query the exported types (i.e. the public classes) using reflection. Everything worked fine, except that the .DLL files were being locked until the application was terminated. The problem is, that once you load an assembly into the main AppDomain using Assembly.LoadFrom(), it stays loaded and you cannot unload the assembly. Also, loading an assembly into the AppDomain where your other code is running might have unexpected side effects. To avoid all these problems, we need to do the work in a secondary, isolated AppDomain. An AppDomain is like […] Read more...

Tags: , , , , ,

How to implement and register a custom dependency resolver in ASP.NET MVC 5

March 6, 2015, (updated on March 26, 2015), Software Development

The ASP.NET MVC 5 framework supports the integration of 3rd party dependency injection frameworks. This can be done by implementing the IDependencyResolver interface and registering an instance of the interface on application startup. The registration is made in the Application_Start method which can be found in the Global.asax.cs file: protected void Application_Start() { ... var compositionContext = new CompositionContext(); compositionContext.AddPart<IMyService, MyServiceImpl>(); // TODO: Register your own services here DependencyResolver.SetResolver(new MyDependencyResolver(compositionContext)); } Most dependency injection frameworks provide a NuGet package which already contains an IDependencyResolver implementation (for example Ninject.MVC3). However, because I wanted to use the IoC container from my own […] Read more...

Tags: , , , , , , ,


  Page 1 of 1