// searches for the class, initiates it (calls factory method) and returns the instance
 // TODO: add a lot of error handling!
 ICalculate CreateCachableICalculate(string className)
 {
     if (!InstanceCreateCache.ContainsKey(className))
     {
         // get the type (several ways exist, this is an eays one)
         Type type = TypeDelegator.GetType("AirportListTest." + className);
         // NOTE: do NOT use the following, because you cannot create a delegate from a ctor!
         ConstructorInfo constructorInfo = type.GetConstructor(Type.EmptyTypes);
         // works with public instance/static methods
         MethodInfo mi = type.GetMethod("Create");
         // the "magic", turn it into a delegate
         var createInstanceDelegate = (Func <ICalculate>)Delegate.CreateDelegate(typeof(Func <ICalculate>), mi);
         // store for future reference
         InstanceCreateCache.Add(className, createInstanceDelegate);
     }
     return(InstanceCreateCache[className].Invoke());
 }
예제 #2
0
        public static T ConvertToObject(DataRow dr, string prefix = "")
        {
            T t = new T();

            PropertyInfo[]       targetProperties = typeof(T).GetProperties();
            DataColumnCollection columns          = dr.Table.Columns;

            foreach (PropertyInfo i in targetProperties)
            {
                var aa = i.PropertyType;
                Console.WriteLine(aa);
                string name = prefix + i.Name;
                if (columns.Contains(name))
                {
                    object value = dr[name];
                    if (value != DBNull.Value)
                    {
                        if (i.PropertyType == typeof(bool))
                        {
                            i.SetValue(t, (true.Equals(value) || "1".Equals(value) || "Y".Equals(value)), null);
                        }
                        else if (i.PropertyType.IsEnum)
                        {
                            i.SetValue(t, Enum.ToObject(i.PropertyType, value));
                        }
                        else
                        {
                            if (i.PropertyType.ToString().Contains("System.Nullable"))
                            {
                                string propertyType = i.PropertyType.ToString().Substring(i.PropertyType.ToString().IndexOf('[') + 1);
                                propertyType = propertyType.Remove(propertyType.LastIndexOf(']'));
                                i.SetValue(t, Convert.ChangeType(value, TypeDelegator.GetType(propertyType)), null);
                            }
                            else
                            {
                                i.SetValue(t, Convert.ChangeType(value, i.PropertyType), null);
                            }
                        }
                    }
                }
            }

            return(t);
        }
예제 #3
0
        /// <summary>
        /// Allows the sprite to handle user input.
        /// </summary>
        /// <param name="input">The input.</param>
        public void OnEnter(Player player)
        {
            // Play the sound and deactivate the door
            this._doorSound.Play();
            this._isActive = false;
            CollisionManager.Collection.QueuePendingRemoval(this);

            // Create the level
            var screenType = TypeDelegator.GetType("SoftProGameWindows." + this._levelScreenName);
            var screen     = (LevelScreen)Activator.CreateInstance(screenType);

            // Add it to the screen manager
            var sm = this.GetService <ScreenManager>();

            sm.AddScreen(screen, null);

            // Spawn the player
            screen.SpawnPlayer(player);
        }
예제 #4
0
        public static T ConvertToObject(object obj)
        {
            T t = new T();

            PropertyInfo[] targetProperties = typeof(T).GetProperties();
            PropertyInfo[] sourceProperties = obj.GetType().GetProperties();

            foreach (PropertyInfo i in targetProperties)
            {
                var sp = (from p in sourceProperties
                          where p.Name == i.Name
                          select p).FirstOrDefault();

                if (null == sp)
                {
                    continue; // property not found
                }
                if (i.PropertyType.Equals(sp.PropertyType))
                {
                    i.SetValue(t, sp.GetValue(obj), null);
                }
                else if (i.PropertyType.ToString().Contains("System.Nullable"))
                {
                    // 取得原始變數類型
                    string propertyType = i.PropertyType.ToString().Substring(i.PropertyType.ToString().IndexOf('[') + 1);
                    propertyType = propertyType.Remove(propertyType.LastIndexOf(']'));
                    i.SetValue(t, Convert.ChangeType(sp.GetValue(obj), TypeDelegator.GetType(propertyType)), null);
                }
                else
                {
                    i.SetValue(t, Convert.ChangeType(sp.GetValue(obj), i.PropertyType), null);
                }
            }

            return(t);
        }