Ejemplo n.º 1
0
        public DataField(FieldDefinition field, object Object)
        {
            this.ContainerObject = Object;
            this.Field           = field;
            if (DynamicTypes.Types.ContainsKey(this.ContainerObject.GetType().FullName))
            {
                this.InitValue = DynamicTypes.Get(this.ContainerObject, field.FieldName);
                Console.WriteLine(Object.GetType().FullName + "_" + field.FieldName + "_" + InitValue);
            }
            else
            {
                this.InitValue = this.ContainerObject.GetType().GetField(field.FieldName).GetValue(this.ContainerObject);
            }
            this._Value = this.InitValue;
            if (field.Extra.ContainsKey("max"))
            {
                this._MaxValue = (double)double.Parse(field.Extra["max"]);
            }
            if (field.Extra.ContainsKey("min"))
            {
                this._MinValue = (double)double.Parse(field.Extra["min"]);
            }

            CheckMinMax();
        }
Ejemplo n.º 2
0
            /// <summary>Searches all loaded assemblies for the first type matching the provided name. Caches to FarmTypeManager.Utility.DynamicTypes for future access.</summary>
            /// <param name="typeName">The full name of the type to be found (e.g. "FarmTypeManager.Monsters.GhostFTM").</param>
            /// <param name="baseClass">If not null, the returned type must be derived from this class.</param>
            /// <returns>Returns the first type found with a matching name, or null if none matched.</returns>
            public static Type GetTypeFromName(string typeName, Type baseClass = null)
            {
                Type matchingType;

                bool filterName(Type type) => type.FullName.Equals(typeName, StringComparison.OrdinalIgnoreCase); //true when a type's full name matches the provided name
                bool filterSubclass(Type type) => type.IsSubclassOf(baseClass);                                   //true when a type is derived from the provided base class
                bool filterInvalidAssemblies(Assembly assembly) =>                                                //true when an assembly can be checked for the desired type (i.e. the assembly should not cause errors when checked)
                assembly.IsDynamic == false &&
                assembly.ManifestModule.Name != "<In Memory Module>" &&
                !assembly.FullName.StartsWith("System") &&
                !assembly.FullName.StartsWith("Microsoft");

                if (baseClass != null) //if a base class was provided
                {
                    //if this type already exists in the DynamicTypes list, retrieve it
                    matchingType = DynamicTypes
                                   .Where(filterSubclass)       //ignore any types that are not subclasses of baseClass
                                   .FirstOrDefault(filterName); //get the first assembly with a matching name (or null if no types matched)

                    if (matchingType == null)                   //if this type isn't in the DynamicTypes list
                    {
                        matchingType =
                            AppDomain.CurrentDomain.GetAssemblies()      //get all assemblies
                            .Where(filterInvalidAssemblies)              //ignore any assemblies that might cause errors when checked this way
                            .SelectMany(assembly => assembly.GetTypes()) //get all types from each assembly as a single sequence
                            .Where(filterSubclass)                       //ignore any types that are not subclasses of baseClass
                            .FirstOrDefault(filterName);                 //get the first assembly with a matching name (or null if no types matched)

                        if (matchingType != null)                        //if a matching type was found
                        {
                            DynamicTypes.Add(matchingType);              //add it to the DynamicTypes list for quicker access
                        }
                    }
                }
                else //if a base class was NOT provided
                {
                    //if this type already exists in the DynamicTypes list, retrieve it
                    matchingType = DynamicTypes
                                   .FirstOrDefault(filterName); //get the first assembly with a matching name (or null if no types matched)

                    if (matchingType == null)                   //if this type isn't in the DynamicTypes list
                    {
                        matchingType =
                            AppDomain.CurrentDomain.GetAssemblies()      //get all assemblies
                            .Where(filterInvalidAssemblies)              //ignore any assemblies that might cause errors when checked this way
                            .SelectMany(assembly => assembly.GetTypes()) //get all types from each assembly as a single sequence
                            .FirstOrDefault(filterName);                 //get the first assembly with a matching name (or null if no types matched)

                        if (matchingType != null)                        //if a matching type was found
                        {
                            DynamicTypes.Add(matchingType);              //add it to the DynamicTypes list for quicker access
                        }
                    }
                }

                return(matchingType);
            }
Ejemplo n.º 3
0
            /// <summary>Searches all loaded assemblies for the first type matching the provided name. Caches to FarmTypeManager.Utility.DynamicTypes for future access.</summary>
            /// <param name="typeName">The full name of the type to be found (e.g. "FarmTypeManager.Monsters.GhostFTM").</param>
            /// <param name="baseClass">If not null, the returned type must be derived from this class.</param>
            /// <returns>Returns the first type found with a matching name, or null if none matched.</returns>
            public static Type GetTypeFromName(string typeName, Type baseClass = null)
            {
                Type matchingType;

                bool filterName(Type type) => type.FullName.Equals(typeName, StringComparison.OrdinalIgnoreCase); //true when a type's full name matches the provided name
                bool filterSubclass(Type type) => type.IsSubclassOf(baseClass);                                   //true when a type is derived from the provided base class

                if (baseClass != null)                                                                            //if a base class was provided
                {
                    //if this type already exists in the DynamicTypes list, retrieve it
                    matchingType = DynamicTypes
                                   .Where(filterSubclass)       //ignore any types that are not subclasses of baseClass
                                   .FirstOrDefault(filterName); //get the first assembly with a matching name (or null if no types matched)

                    if (matchingType == null)                   //if this type isn't in the DynamicTypes list
                    {
                        matchingType =
                            AppDomain.CurrentDomain.GetAssemblies()         //get all assemblies
                            .Where(assembly => assembly.IsDynamic == false) //ignore any dynamic assemblies
                            .SelectMany(assembly => assembly.GetTypes())    //get all types from each assembly as a single sequence
                            .Where(filterSubclass)                          //ignore any types that are not subclasses of baseClass
                            .FirstOrDefault(filterName);                    //get the first assembly with a matching name (or null if no types matched)

                        if (matchingType != null)                           //if a matching type was found
                        {
                            DynamicTypes.Add(matchingType);                 //add it to the DynamicTypes list for quicker access
                        }
                    }
                }
                else //if a base class was NOT provided
                {
                    //if this type already exists in the DynamicTypes list, retrieve it
                    matchingType = DynamicTypes
                                   .FirstOrDefault(filterName); //get the first assembly with a matching name (or null if no types matched)

                    if (matchingType == null)                   //if this type isn't in the DynamicTypes list
                    {
                        matchingType =
                            AppDomain.CurrentDomain.GetAssemblies()         //get all assemblies
                            .Where(assembly => assembly.IsDynamic == false) //ignore any dynamic assemblies
                            .SelectMany(assembly => assembly.GetTypes())    //get all types from each assembly as a single sequence
                            .FirstOrDefault(filterName);                    //get the first assembly with a matching name (or null if no types matched)

                        if (matchingType != null)                           //if a matching type was found
                        {
                            DynamicTypes.Add(matchingType);                 //add it to the DynamicTypes list for quicker access
                        }
                    }
                }

                return(matchingType);
            }
Ejemplo n.º 4
0
        public Assembly Compile(AssemblyName assemblyName)
        {
            var assemblyBuilder =
                AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);

            foreach (var attribute in Attributes)
            {
                assemblyBuilder.SetCustomAttribute(AnnotationAttributeBuilder.Create(attribute));
            }

            var moduleName = string.Format("DynamicEntitiesModule{0}.dll", _assemblyCount);
            var module     = assemblyBuilder.DefineDynamicModule(moduleName);

            _assemblyCount++;

            foreach (var enumTypeInfo in DynamicTypes.OfType <DynamicEnumType>())
            {
                _types.Add(enumTypeInfo.Name, DefineEnumType(module, enumTypeInfo));
            }

            foreach (var typeInfo in DynamicTypes.OfType <DynamicStructuralType>())
            {
                DefineStructuralType(module, typeInfo);
            }

            foreach (var typeInfo in DynamicTypes.OfType <DynamicStructuralType>())
            {
                var typeBuilder = _typeBuilders[typeInfo.Name];

                foreach (var fieldInfo in typeInfo.Fields)
                {
                    DefineField(typeBuilder.Item1, typeBuilder.Item2, fieldInfo);
                }

                foreach (var propertyInfo in typeInfo.Properties)
                {
                    DefineProperty(typeBuilder.Item1, propertyInfo);
                }
            }

            foreach (var t in _typeBuilders)
            {
                _types.Add(t.Key, t.Value.Item1.CreateType());
            }

            return(assemblyBuilder);
        }
Ejemplo n.º 5
0
 public void Save()
 {
     DynamicTypes.Set(this.ContainerObject, Field.FieldName, this.Value);
 }
Ejemplo n.º 6
0
        // the entry point for all C# programs. The Main method states what the class does when executed.
        static void Main(string[] args)
        {
            #region part1 - Very short presentation, Hello World, class structure, using position in file

            Console.WriteLine("Part 1: \n");

            // my first program in C#

            // WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.
            Console.WriteLine("Hello World");

            // This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio.
            Console.ReadKey();

            #endregion


            #region part2 - Class, members, methodes, constructors, override ToString()

            Console.WriteLine("\n\n\nPart 2: \n");

            // initialization methods:
            // 1:
            var rectangle1 = new Fundamentals2016.Part2.Rectangle();
            rectangle1.Length = 10;
            rectangle1.Width  = 6;

            // 2:
            var rectangle2 = new Fundamentals2016.Part2.Rectangle(12, 8);

            // 3:
            var rectangle3 = new Fundamentals2016.Part2.Rectangle();
            rectangle3.InitializeWithCustomValues();

            // 4:
            var rectangle4 = new Fundamentals2016.Part2.Rectangle();
            rectangle4.InitializeWithSpecificValues(20, 15);

            // call methods:
            Console.WriteLine(rectangle1.GetArea());
            rectangle1.Display();
            Console.WriteLine(rectangle1.ToString());
            Console.ReadKey();

            Console.WriteLine(rectangle2.ToString());
            Console.ReadKey();

            Console.WriteLine(rectangle3.ToString());
            Console.ReadKey();

            Console.WriteLine(rectangle4.ToString());
            Console.ReadKey();

            #endregion


            #region part3 - Value type, Referance type, boxing, unboxing, Dynamic type, static methodes, convertions, constant variables, readonly variables

            Console.WriteLine("Size of int: {0}", sizeof(int));
            Console.ReadKey();

            // boxing & unboxing
            object obj;
            obj = 100;               // this is boxing
            int unboxObj = (int)obj; // this is unboxing

            // cast double to int.
            double nr1 = 5673.74;
            int    nr2;
            nr2 = (int)nr1;
            Console.WriteLine(nr2);
            Console.ReadKey();

            // dinamyc type: (with static methode)
            Console.WriteLine();
            DynamicTypes.DynamicTypesExample();

            // string conversion: (with static methode)
            Console.WriteLine();
            StringConversion.ConvertValues();

            // working with constants:
            Console.WriteLine("\n");
            Console.WriteLine("Enter Radius: ");
            var radius = Convert.ToDouble(Console.ReadLine());
            var circle = new CircleExampleWithConstants(radius);
            circle.DisplayArea();

            #endregion


            #region part4 - enums, if (?), ??, switch, inheritance, interface, IComparable, generic methods, overloading, polymorphism

            var triangle        = new Triangle(3, 4, 5);
            var rectangle       = new Fundamentals2016.Part4.Rectangle(2, 6);
            var regularPentagon = new RegularPentagon(4);

            Console.WriteLine("\n\n " + triangle.ToString() + ": Perimetru: " + PoligonHelper.GetPerimeter(triangle) + "  Arie: " + PoligonHelper.GetAria(triangle));
            Console.WriteLine("\n\n " + rectangle.ToString() + ": Perimetru: " + PoligonHelper.GetPerimeter(rectangle) + "  Arie: " + PoligonHelper.GetAria(rectangle));
            Console.WriteLine("\n\n " + regularPentagon.ToString() + ": Perimetru: " + PoligonHelper.GetPerimeter(regularPentagon) + "  Arie: " + PoligonHelper.GetAria(regularPentagon));
            Console.ReadKey();

            // polimophism:
            Poligon p = new Fundamentals2016.Part4.Rectangle(2, 6);
            Console.WriteLine("\n\n " + p.GetType() + ": Perimetru: " + p.CalculatePerimeter());
            Console.ReadKey();

            #endregion


            #region part5 - using, read from file, write in file, exceptions, custom exception, threads, async await

            var file = new WorkWithFileExample("E:\\ZTH\\TextFile.txt");
            file.ReadContent();
            file.WriteContent();

            // custom exception:
            Console.WriteLine("\n");
            Console.WriteLine("Enter a number: ");
            var number = Convert.ToDouble(Console.ReadLine());
            if (number < 0)
            {
                //throw new NegativeNumberException();
                throw new NegativeNumberException("Negative number is not allowed!");
            }
            Console.ReadKey();

            // events:

            // threads, async, await:

            #endregion
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            var exampleToRun = ExamplesEnumeration.JoinWithExtensionMethodsSyntax;

            switch (exampleToRun)
            {
            case ExamplesEnumeration.BasicSyntax:
                BasicSyntax.ShowBasicSyntaxLinq();
                break;

            case ExamplesEnumeration.GenderCondition:
                BasicSyntax.ShowConditionWithGender();
                break;

            case ExamplesEnumeration.ExtensionMethodSyntax:
                ExtensionMethodSyntax.ShowExtensionMethodSyntax();
                break;

            case ExamplesEnumeration.DefferedExecution:
                ExtensionMethodSyntax.DefferedExecution();
                break;

            case ExamplesEnumeration.WhereBasicSyntax:
                WhereClause.ShowWhereWithBasicSyntax();
                break;

            case ExamplesEnumeration.WhereExtensionSyntax:
                WhereClause.ShowWhereWithExtensionSyntax();
                break;

            case ExamplesEnumeration.OrderByBasicSyntax:
                OrderByClause.ShowOrderBy();
                break;

            case ExamplesEnumeration.OrderByExtensionSyntax:
                OrderByClause.OrderByExtensionSyntax();
                break;

            case ExamplesEnumeration.GroupByBasicSyntax:
                GroupByClause.ShowGroupBy();
                break;

            case ExamplesEnumeration.GroupByExtensionSyntax:
                GroupByClause.GroupByExtensionSyntax();
                break;

            case ExamplesEnumeration.GroupByInto:
                GroupByClause.GroupByInto();
                break;

            case ExamplesEnumeration.AnonymousTypes:
                AnonymousTypes.ShowAnonymousTypes();
                break;

            case ExamplesEnumeration.AnonymousTypesTryChangeProperty:
                AnonymousTypes.TryChangeProperty();
                break;

            case ExamplesEnumeration.AnonymousTypesPropertyNamesInheritance:
                AnonymousTypes.PropertiesNamesInheritance();
                break;

            case ExamplesEnumeration.ExplicitSelectClause:
                ExplicitSelectClause.ShowExplicitSelect();
                break;

            case ExamplesEnumeration.ExplicitSelectAnonymousType:
                ExplicitSelectClause.SelectAnonymousType();
                break;

            case ExamplesEnumeration.DynamicTypes:
                DynamicTypes.ShowDynamicTypes();
                break;

            case ExamplesEnumeration.JoinWithQuerySyntax:
                JoinClause.ShowJoinWithQuerySyntax();
                break;

            case ExamplesEnumeration.JoinWithExtensionMethodsSyntax:
                JoinClause.ShowJoinWithExtensionMethodsSyntax();
                break;
            }

            Console.Read();
        }
Ejemplo n.º 8
0
        // the entry point for all C# programs. The Main method states what the class does when executed.
        static void Main(string[] args)
        {
            #region part1 - Very short presentation, Hello World, class structure

            Console.WriteLine("Part 1: \n");

            // my first program in C#

            // WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.
            Console.WriteLine("Hello World ");

            // This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio.
            Console.ReadKey();

            #endregion


            #region part2 - class, namespace, dynamic type, static methodes, convertions, constant, readonly, struct, generics, enums

            Console.WriteLine("\n\n\nPart 2: \n");


            #region simple class example

            // initialization methods:
            // 1:
            var rectangle1 = new Rectangle
            {
                Length = 10,
                Width  = 6
            };

            // 2:
            var rectangle2 = new Rectangle(12, 8);

            // 3:
            var rectangle3 = new Rectangle();
            rectangle3.InitializeWithCustomValues();

            // 4:
            var rectangle4 = new Rectangle();
            rectangle4.InitializeWithSpecificValues(20, 15);

            // call methods:
            Console.WriteLine(rectangle1.GetArea());
            rectangle1.Display();
            Console.WriteLine(rectangle1.ToString());
            Console.ReadKey();

            Console.WriteLine(rectangle2.ToString());
            Console.ReadKey();

            Console.WriteLine(rectangle3.ToString());
            Console.ReadKey();

            Console.WriteLine(rectangle4.ToString());
            Console.ReadKey();

            // to do: GetPerimeter();

            #endregion


            #region namespace example

            Test3.MyMethode();

            #endregion


            #region satatic class, static methods, dynamic type example

            // dinamyc type: (with static class, static methode)
            Console.WriteLine();
            DynamicTypes.DynamicTypesExample();

            #endregion


            #region var, boxing and unboxing, conversion

            var    val = 1;
            object obj = val;      // boxing;
            int    i   = (int)obj; // unboxing;

            // cast double to int.
            double nr1 = 5673.74;
            int    nr2 = (int)nr1;
            Console.WriteLine("nr = {0}, type = {1}", nr1, nr1.GetType());
            Console.WriteLine("nr = {0}, type = {1}", nr2, nr2.GetType());
            Console.ReadKey();

            #endregion


            #region const, readonly

            // working with constants:
            Console.WriteLine("\n");
            Console.WriteLine("Enter Radius: ");
            try
            {
                var radius = Convert.ToDouble(Console.ReadLine());
                var circle = new CircleExampleWithConstants(radius);
                circle.DisplayArea();
            }
            catch (FormatException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }

            #endregion

            // struct
            // generic types
            // enums
            // extension methods

            // to do: extension method pe int, ridicare la patrat

            #endregion


            #region part3 - inheritance, interface, IComparable, generic  methods, overloading, polymorphism


            #endregion


            #region part4 - using, read from file, write in file, exceptions, custom exception, threads, async await

            var file = new WorkWithFileExample("E:\\ZTH\\TextFile.txt");
            file.ReadContent();
            file.WriteContent();

            // custom exception:
            Console.WriteLine("\n");
            Console.WriteLine("Enter a number: ");


            var number = Convert.ToDouble(Console.ReadLine());
            if (number < 0)
            {
                //throw new NegativeNumberException();
                throw new NegativeNumberException("Negative number is not allowed!");
            }
            Console.ReadKey();

            // events:

            // threads, async, await:

            #endregion
        }