Third usage of C# out keyword with Roslyn Intro

Hi, today I want to share with you innovative solution idea I want to make with Visual Studio 14 CTP that I found by looking to Roslyn project on CodePlex site. So what is the case here? Do you remember one of my last blog entry about Generic Free Pattern. That was solution that can be used for making C# similar to JavaScript when you write the code, but type safe when compiler use it the same time. So what is the case of third usage of out keyword? Let say we have generic class with generic type argument T and we have constructor that needs argument with type T. I would like to make something impossible right now witch is make generic constructor with “out T” as generic argument. I have no better idea than transform that to Generic Free Pattern. Se let me start with code sample that cannot be compiled… cannot be yet :).

namespace GenericFreeSandbox
{
    using System;

    public interface IEntity {
        string Name { get; set; }
    }

    public class Entity : IEntity {
        public string Name { get; set; }
    }

    public class EntityManager<T> where T : IEntity
    {
        private readonly T entity;

        public EntityManager<out T>(T entity) {
            this.entity = entity; 
        }

        public string GetName() {
            return entity.Name;
        }
    }

    public class EntityManagerTester {
        public static bool GetName() {
            var entity = new GenericFreeSandbox.Entity
            { Name = "Piotr Sowa's Entity for Roslyn Compiler Extension" };
            var manager = new GenericFreeSandbox.EntityManager(entity);
            var name = manager.GetName();
            return name == "Piotr Sowa's Entity for Roslyn Compiler Extension";
        }
    }
}

As you can see the innovation of above code is the “out T” generic argument for constructor for the EntityManager class. Please look on the EntityManagerTester class with usage of this solution. Are you get it? I instantiated EntityManager with entity without generic parameter. My intention was that EntityManager will figure out its own generic type by out modificator from Entity type given for constructor. I will also show you example of run this code to make shure it is valid. I like to invent new and different way for testing code. So here it is.

namespace GenericFreeCompilerAndRunner {
    using System;

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(
                "TEST EntityManagerTester.GetName() {0}",
                 EntityManagerTester.GetName() == true ? "PASS" : "FAIL"
            );
        }
    }
}

I expect that the way of doing my solution will be SyntaxTree.ParseCompilationUnit(code) method to get syntax tree and then change/transform it to use Generic Free Pattern. I do not have solution today, but I like to make it nice and share with you. I hope you like this idea. One of the nice usage is for Dependency Injection solutions and framework classes that automaticaly figureout behaviour by given constructor argument. Without using Roslyn C# code cannot be compiled. See you next time on CodingByToDesign.NET :).

p ;).

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.