Ejemplo n.º 1
0
        public void TestInitialize()
        {
            this.conversions = new UserConversions();

            this.createAccountViewModel = new CreateAccountViewModel
            {
                Id              = 1,
                Password        = "******",
                ConfirmPassword = "******",
                FirstName       = "First",
                LastName        = "Last",
                UserName        = "******",
                UserType        = UserRoleType.Admin
            };

            this.user = new User
            {
                ID             = 1,
                Contact        = new Contact(),
                FirstName      = "First",
                LastName       = "Last",
                UserCredential = new UserCredential {
                    ID = 123, PasswordHash = "a298rasdfjkhn", UserName = "******", ResetToken = Guid.NewGuid().ToString()
                },
                UserRoles = new List <UserRole> {
                    new UserRole {
                        RoleTypeID = 1, UserID = 1
                    }
                },
                ServiceProviderID = null,
                IsActive          = true
            };

            this.accountAdminViewModel = new AccountAdminViewModel
            {
                UserId       = 1,
                Email        = "*****@*****.**",
                FirstName    = "First",
                LastName     = "Last",
                IsActive     = true,
                ProviderId   = null,
                ProviderName = string.Empty,
                Role         = UserRoleType.Admin
            };
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AccountLogics"/> class.
 /// </summary>
 public AccountLogics()
 {
     this.userConversions = new UserConversions();
     this.userRepo        = new UserRepo();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// 返回与指定类型相关的用户自定义类型转换方法。基类中声明的类型转换方法也包含在内。
        /// </summary>
        /// <param name="type">要获取类型转换方法的类型。</param>
        /// <returns>与指定类型相关的用户自定义类型转换方法,如果不存在则为 <c>null</c>。</returns>
        private static UserConversions GetUserConversions(Type type)
        {
            Contract.Requires(type != null);
            TypeCode typeCode = Type.GetTypeCode(type);

            if (typeCode != TypeCode.Object && typeCode != TypeCode.Decimal)
            {
                // 其余基本类型都不包含类型转换运算符。
                return(null);
            }
            if (!type.IsClass && !type.IsValueType)
            {
                // 只有类和结构体中能声明类型转换运算符。
                return(null);
            }
            if (type == typeof(object))
            {
                return(null);
            }
            return(conversions.GetOrAdd(type, searchType =>
            {
                List <UserConversionMethod> list = new List <UserConversionMethod>();
                MethodInfo[] methods = searchType.GetMethods(TypeExt.PublicStaticFlag);
                int convertToIndex = 0;
                for (int i = 0; i < methods.Length; i++)
                {
                    MethodInfo method = methods[i];
                    if (!method.Name.Equals(MethodExt.ImplicitMethodName, StringComparison.Ordinal) &&
                        !method.Name.Equals(MethodExt.ExplicitMethodName, StringComparison.Ordinal))
                    {
                        continue;
                    }
                    if (method.ReturnType == type)
                    {
                        // 转换自其它类型。
                        list.Insert(convertToIndex, new UserConversionMethod(method));
                        convertToIndex++;
                    }
                    else
                    {
                        // 转换到其它类型。
                        list.Add(new UserConversionMethod(method));
                    }
                }
                // 基类包含的转换,子类也可以使用。
                if (type.IsClass)
                {
                    UserConversions baseConv = GetUserConversions(type.BaseType);
                    if (baseConv != null)
                    {
                        if (list.Count == 0)
                        {
                            return baseConv;
                        }
                        int cnt = baseConv.ConvertToIndex;
                        if (cnt > 0)
                        {
                            list.InsertRange(convertToIndex, baseConv.Methods.Take(cnt));
                            convertToIndex += cnt;
                        }
                        if (cnt < baseConv.Methods.Length)
                        {
                            list.AddRange(baseConv.Methods.Skip(cnt));
                        }
                    }
                }
                if (list.Count == 0)
                {
                    return null;
                }
                return new UserConversions(list.ToArray(), convertToIndex);
            }));
        }
Ejemplo n.º 4
0
            /// <summary>
            /// 查找合适的用户自定义类型转换方法。
            /// </summary>
            /// <returns>合适的用户自定义类型转换方法,如果不存在则为 <c>null</c>。</returns>
            public MethodInfo FindConversion()
            {
                UserConversions convs = GetUserConversions(inputType);

                if (convs != null)
                {
                    Contract.Assume(convs.ConvertToIndex >= 0);
                    for (int i = convs.ConvertToIndex; i < convs.Methods.Length; i++)
                    {
                        UserConversionMethod method = convs.Methods[i];
                        ConversionType       ctype  = ConversionFactory.GetStandardConversion(outputType, method.OutputType);
                        if (ctype == ConversionType.None)
                        {
                            continue;
                        }
                        TypeRelation inputRelation = (method.InputType == inputType) ?
                                                     TypeRelation.Best : TypeRelation.Second;
                        TypeRelation outputRelation = TypeRelation.Best;
                        if (ctype >= ConversionType.ExplicitNumeric)
                        {
                            outputRelation = TypeRelation.Second;
                        }
                        else if (ctype > ConversionType.Identity)
                        {
                            outputRelation = TypeRelation.Thirt;
                        }
                        GetBestConversion(method, inputRelation, outputRelation);
                    }
                }
                convs = GetUserConversions(outputType);
                if (convs != null)
                {
                    for (int i = 0; i < convs.ConvertToIndex; i++)
                    {
                        UserConversionMethod method = convs.Methods[i];
                        ConversionType       ctype  = ConversionFactory.GetStandardConversion(method.InputType, inputType);
                        if (ctype == ConversionType.None)
                        {
                            continue;
                        }
                        TypeRelation outputRelation = (method.OutputType == outputType) ?
                                                      TypeRelation.Best : TypeRelation.Second;
                        TypeRelation inputRelation = TypeRelation.Best;
                        if (ctype >= ConversionType.ExplicitNumeric)
                        {
                            inputRelation = TypeRelation.Second;
                        }
                        else if (ctype > ConversionType.Identity)
                        {
                            inputRelation = TypeRelation.Thirt;
                        }
                        GetBestConversion(method, inputRelation, outputRelation);
                    }
                }
                if (bestMethod.IsUnique)
                {
                    return(bestMethod.Value.Method);
                }
                if (bestMethod.IsAmbig)
                {
                    throw CommonExceptions.AmbiguousUserDefinedConverter(inputType, outputType);
                }
                return(null);
            }