Click here to Skip to main content
15,891,513 members
Articles / Programming Languages / C# 4.0

Units of Measure Validator for C#

Rate me:
Please Sign up or sign in to vote.
4.66/5 (28 votes)
2 Jul 2012CPOL7 min read 81.3K   2.4K   41  
This library provides a MSBuild task for compile time validation of units of measurement within C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.NRefactory.CSharp;
using System.IO;
using System.Text.RegularExpressions;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.NRefactory.CSharp.TypeSystem;
using ICSharpCode.NRefactory.CSharp.Resolver;
using ICSharpCode.NRefactory.Semantics;

namespace HDLibrary.UnitsOfMeasure.Validator
{
    class SharedElementScope
    {
        private Dictionary<object, SharedElement> sharedElements = new Dictionary<object, SharedElement>();

        public ISharedElement GetSharedElement(ResolveResult resolveResult, out bool isNew)
        {
            object key = null;
            MemberResolveResult mrr = resolveResult as MemberResolveResult;
            if (mrr != null)
                key = mrr.Member;
            else
            {
                LocalResolveResult lrr = resolveResult as LocalResolveResult;
                if (lrr != null)
                    key = lrr.Variable;
            }

            isNew = false;

            if (key == null)
                return null;

            SharedElement result;

            if (!sharedElements.TryGetValue(key, out result))
            {
                result = new SharedElement();
                sharedElements.Add(key, result);
                isNew = true;
            }

            return result;
        }

        private class SharedElement : ISharedElement
        {
            public IUnitDescription UnitDescription { get; set; }
            //public Unit Unit { get; set; }
            //public Unit[] Parameters { get; set; }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
Presently I am a student of computer science at the Karlsruhe Institute of Technology in Germany.

Comments and Discussions