Click here to Skip to main content
15,895,799 members
Home / Discussions / C#
   

C#

 
QuestionAppropriate solution on reading a list of Strings from a file Pin
nstk6-Apr-11 21:35
nstk6-Apr-11 21:35 
AnswerRe: Appropriate solution on reading a list of Strings from a file Pin
OriginalGriff6-Apr-11 22:30
mveOriginalGriff6-Apr-11 22:30 
GeneralRe: Appropriate solution on reading a list of Strings from a file Pin
nstk7-Apr-11 4:34
nstk7-Apr-11 4:34 
AnswerRe: Appropriate solution on reading a list of Strings from a file Pin
PIEBALDconsult7-Apr-11 3:07
mvePIEBALDconsult7-Apr-11 3:07 
GeneralRe: Appropriate solution on reading a list of Strings from a file [modified] Pin
nstk7-Apr-11 4:37
nstk7-Apr-11 4:37 
AnswerRe: Appropriate solution on reading a list of Strings from a file Pin
OriginalGriff7-Apr-11 8:54
mveOriginalGriff7-Apr-11 8:54 
GeneralRe: Appropriate solution on reading a list of Strings from a file Pin
nstk7-Apr-11 10:48
nstk7-Apr-11 10:48 
GeneralRe: Appropriate solution on reading a list of Strings from a file Pin
PIEBALDconsult7-Apr-11 15:26
mvePIEBALDconsult7-Apr-11 15:26 
Oh, one of those -- an excellent exercise.

According to the file date, I wrote the following last October (probably in response to a post here):

namespace PIEBALD.Lib.LibExt.Convert
{
    public interface IConvert
    {
        double Convert ( double Value ) ;
    }

    public enum Language
    {
        CSharp
    ,
        VisualBasic
    }

    [System.AttributeUsageAttribute(System.AttributeTargets.Field , AllowMultiple=false , Inherited=false)]
    public sealed class ConverterAttribute : System.Attribute
    {
        public ConverterAttribute
        (
            string   Function
        ,
            Language Language
        )
        {
            this.Function = Function ;

            this.Language = Language ;

            return ;
        }

        public string Function { get ; private set ; }

        public Language Language { get ; private set ; }
    }

    public enum Conversion
    {
        [ConverterAttribute("Value * 2.54",Language.VisualBasic)]
        FromInchToCentimeter
    ,
        [ConverterAttribute("Value / 2.54",Language.VisualBasic)]
        FromCentimeterToInch
    ,
        [ConverterAttribute("( Value - 32.0 ) * 5.0 / 9.0",Language.CSharp)]
        FromFahrenheitToCelsius
    ,
        [ConverterAttribute("Value * 9.0 / 5.0 + 32.0",Language.CSharp)]
        FromCelsiusToFahrenheit
    }

    public static class LibExt
    {
        private static readonly System.Collections.Generic.Dictionary<Conversion,IConvert> conversion ;

        static LibExt
        (
        )
        {
            conversion = new System.Collections.Generic.Dictionary<Conversion,IConvert>() ;

            return ;
        }

        private static void
        Add
        (
            Conversion Conversion
        )
        {
            System.Reflection.FieldInfo fi = typeof(Conversion).GetField
            (
                Conversion.ToString()
            ,
                System.Reflection.BindingFlags.Public
                |
                System.Reflection.BindingFlags.Static
            ) ;

            foreach
            (
                ConverterAttribute att
            in
                fi.GetCustomAttributes ( typeof(ConverterAttribute) , false ) 
            )
            {
                string code = null ;

                switch ( att.Language )
                {
                    case Language.CSharp :
                    {
                        code = System.String.Format
                        (
                            @"
                            namespace Converter
                            {{
                                public class Converter : PIEBALD.Lib.LibExt.Convert.IConvert
                                {{
                                    public double
                                    Convert
                                    (
                                        double Value
                                    )
                                    {{
                                        return ( {0} ) ;
                                    }}
                                }}
                            }}
                            "
                        ,
                            att.Function
                        ) ;

                        break ;
                    }

                    case Language.VisualBasic :
                    {
                        code = System.String.Format
                        (
                            @"
                            namespace Converter
                                public class Converter
                                    implements PIEBALD.Lib.LibExt.Convert.IConvert

                                    public function Convert ( byval Value as double ) as double _
                                        implements PIEBALD.Lib.LibExt.Convert.IConvert.Convert

                                        return ( {0} ) 
                                    end function
                                end class
                            end namespace
                            "
                        ,
                            att.Function
                        ) ;

                        break ;
                    }
                }

                System.Reflection.Assembly assm = PIEBALD.Lib.LibSys.Compile
                (
                    code
                ,
                    att.Language.ToString()
                ,
                    System.Reflection.Assembly.GetExecutingAssembly().Location
                ) ;

                conversion [ (Conversion) fi.GetValue ( null ) ] =
                    (IConvert) assm.CreateInstance ( "Converter.Converter" ) ;
            }

            return ;
        }

        public static double
        Convert
        (
            this double Value
        ,
            Conversion  Conversion
        )
        {
            if ( !conversion.ContainsKey ( Conversion ) )
            {
                Add ( Conversion ) ;
            }

            return ( conversion [ Conversion ].Convert ( Value ) ) ;
        }
    }
}



Obviously it's not extensible they way you want, but it may give you some ideas.

You should also look at this[^].


May the force be with you. Thumbs Up | :thumbsup:
QuestionKilling hidden processes Pin
teknolog1236-Apr-11 9:43
teknolog1236-Apr-11 9:43 
AnswerRe: Killing hidden processes Pin
Luc Pattyn6-Apr-11 13:51
sitebuilderLuc Pattyn6-Apr-11 13:51 
GeneralRe: Killing hidden processes Pin
Rajesh R Subramanian7-Apr-11 6:52
professionalRajesh R Subramanian7-Apr-11 6:52 
GeneralRe: Killing hidden processes Pin
Luc Pattyn7-Apr-11 6:59
sitebuilderLuc Pattyn7-Apr-11 6:59 
GeneralRe: Killing hidden processes Pin
Pete O'Hanlon7-Apr-11 9:30
mvePete O'Hanlon7-Apr-11 9:30 
GeneralRe: Killing hidden processes Pin
Luc Pattyn7-Apr-11 10:04
sitebuilderLuc Pattyn7-Apr-11 10:04 
GeneralRe: Killing hidden processes Pin
Pete O'Hanlon7-Apr-11 10:09
mvePete O'Hanlon7-Apr-11 10:09 
GeneralRe: Killing hidden processes Pin
Luc Pattyn7-Apr-11 10:52
sitebuilderLuc Pattyn7-Apr-11 10:52 
AnswerRe: Killing hidden processes Pin
Bassam Abdul-Baki6-Apr-11 14:09
professionalBassam Abdul-Baki6-Apr-11 14:09 
GeneralRe: Killing hidden processes Pin
thatraja6-Apr-11 17:56
professionalthatraja6-Apr-11 17:56 
AnswerRe: Killing hidden processes Pin
Dave Kreskowiak6-Apr-11 14:38
mveDave Kreskowiak6-Apr-11 14:38 
AnswerRe: Killing hidden processes Pin
dan!sh 6-Apr-11 17:29
professional dan!sh 6-Apr-11 17:29 
GeneralRe: Killing hidden processes Pin
teknolog1237-Apr-11 4:02
teknolog1237-Apr-11 4:02 
GeneralRe: Killing hidden processes Pin
dan!sh 7-Apr-11 5:41
professional dan!sh 7-Apr-11 5:41 
AnswerRe: Killing hidden processes Pin
Rajesh R Subramanian7-Apr-11 6:49
professionalRajesh R Subramanian7-Apr-11 6:49 
GeneralRe: Killing hidden processes Pin
Pete O'Hanlon7-Apr-11 9:32
mvePete O'Hanlon7-Apr-11 9:32 
GeneralRe: Killing hidden processes Pin
Rajesh R Subramanian7-Apr-11 9:36
professionalRajesh R Subramanian7-Apr-11 9:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.