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

Ordering Items in the Property Grid

Rate me:
Please Sign up or sign in to vote.
4.90/5 (73 votes)
5 Apr 2004CPOL3 min read 206.6K   7.3K   81  
A simple custom attribute to order properties in the PropertyGrid.
//
// (C) Paul Tingey 2004 
//
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;

namespace OrderedPropertyGrid
{
    public class CompileEngine
    {
        //
        // Thanks to jconwell for a great sample on how to do this!
        // see http://www.codeproject.com/dotnet/DotNetScript.asp
        // This code is based on that sample.
        //
        static internal Assembly CreateAssembly(string sourceCode)
        {
            CodeDomProvider codeDomProvider = new CSharpCodeProvider();
            ICodeCompiler codeCompiler = codeDomProvider.CreateCompiler();
            //
            // Compiler parameters
            //
            CompilerParameters compilerParams = new CompilerParameters();
            compilerParams.CompilerOptions = "/target:library";
            compilerParams.GenerateExecutable = false;
            compilerParams.GenerateInMemory = true;			
            compilerParams.IncludeDebugInformation = false;
            compilerParams.ReferencedAssemblies.Add( "mscorlib.dll");
            compilerParams.ReferencedAssemblies.Add( "System.dll");			
            compilerParams.ReferencedAssemblies.Add( "OrderedPropertyGridDemo.exe" );
            //
            // Compile the code
            //
            CompilerResults compilerResults = codeCompiler.CompileAssemblyFromSource(compilerParams, sourceCode);
            //
            // Show a message box if any errors
            //
            if (compilerResults.Errors.Count > 0)
            {
                StringBuilder sb = new StringBuilder("Compile failed. ");
                foreach (CompilerError error in compilerResults.Errors)
                {
                    sb.AppendFormat("Error: {0}\n",error.ErrorText);                    
                }
                MessageBox.Show(sb.ToString(),"Compile failed",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return null;
            }
            return compilerResults.CompiledAssembly;
        }
    }
}

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
Software Developer (Senior)
New Zealand New Zealand
Working with code since 1994. Mostly c#, with history in VC++, Embedded C++, Delphi, and Modular 2. Working in the finanical sector.

Comments and Discussions