示例#1
0
        public static PropertyBuilder BuildProperty(this TypeBuilder typeBuilder, string name, Type type, IEnumerable<Type> interfaces)
        {
            var fromInterface = interfaces.ContainsProperty(name);
            // Field
            var fieldBuilder = typeBuilder.DefineField("_" + name, type, FieldAttributes.Private);
            // Property
            var propBuilder = typeBuilder.DefineProperty(name, PropertyAttributes.HasDefault, type, null);
            // Getter
            var getterBuilder = typeBuilder.DefineMethod(
                    string.Concat("get_", name),
                    MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig |
                    MethodAttributes.Virtual,
                    type,
                    Type.EmptyTypes);
            var getterIL = getterBuilder.GetILGenerator();
            getterIL.Emit(OpCodes.Ldarg_0);
            getterIL.Emit(OpCodes.Ldfld, fieldBuilder);
            getterIL.Emit(OpCodes.Ret);
            if (fromInterface)
            {
                typeBuilder.DefineMethodOverride(getterBuilder, interfaces.FindGetter(name));
            }
            //Setter
            var setterBuilder = typeBuilder.DefineMethod(
                string.Concat("set_", name),
                MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig |
                MethodAttributes.Virtual,
                null,
                new[] { type });
            var setterIL = setterBuilder.GetILGenerator();
            setterIL.Emit(OpCodes.Ldarg_0);
            setterIL.Emit(OpCodes.Ldarg_1);
            setterIL.Emit(OpCodes.Stfld, fieldBuilder);
            setterIL.Emit(OpCodes.Ret);
            if (fromInterface)
            {
                typeBuilder.DefineMethodOverride(setterBuilder, interfaces.FindSetter(name));
            }

            propBuilder.SetGetMethod(getterBuilder);
            propBuilder.SetSetMethod(setterBuilder);
            return propBuilder;
        }
示例#2
0
文件: Poker.cs 项目: middas/Poker
        private static bool CheckStraight(IEnumerable<Card> cards, List<Card> bestCards)
        {
            bool isStraight = CheckAceHighStraight(cards);

            if (!isStraight)
            {
                int count = 1; //set to 1 to include the first card

                var list = cards.OrderByDescending(c => c.NumberValue).DistinctBy(c => c.NumberValue).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    if (i + 1 >= list.Count)
                    {
                        break;
                    }

                    if (list[i].NumberValue - list[i + 1].NumberValue == 1)
                    {
                        count++;
                    }
                    else
                    {
                        count = 1;
                    }

                    if (count >= 5)
                    {
                        isStraight = true;

                        i++;

                        while (bestCards.Count < 5)
                        {
                            bestCards.Add(list[i]);
                            i--;
                        }

                        break;
                    }
                }

                if (isStraight && cards.ContainsProperty(c => c.Number == "A"))
                {
                    cards.Where(c => c.Number == "A").ToList().ForEach(c => c.Points = 1);
                }
            }
            else
            {
                bestCards.AddRange(cards.TakeWhile(c =>
                    c.Number == "A" || c.Number == "K" || c.Number == "Q" || c.Number == "J" ||
                    c.Number == "10"));
                bestCards = bestCards.DistinctBy(c => c.Number).ToList();
            }

            return isStraight;
        }
示例#3
0
文件: Poker.cs 项目: middas/Poker
 private static bool CheckAceHighStraight(IEnumerable<Card> cards)
 {
     return cards.ContainsProperty(c => c.Number == "A")
                             && cards.ContainsProperty(c => c.Number == "K")
                             && cards.ContainsProperty(c => c.Number == "Q")
                             && cards.ContainsProperty(c => c.Number == "J")
                             && cards.ContainsProperty(c => c.Number == "10");
 }