zoukankan      html  css  js  c++  java
  • 从表单为实体对象赋值

    首先需要把从表单获得的字符串转换为指定类型,可以使用以下代码:

    object value = Convert.ChangeType(valueStr, desiredType);
    // 把valueStr转换为desiredType指定的类型

    注意:Convert.ChangeType不能转换Nullable<Type>类型的值。需要进行处理以下函数来转换:

    1. 方式一,使用泛型
    		public static T ChangeType<T>(object value)
    		{
    			Type conversionType = typeof(T);
    
    			if (conversionType.IsGenericType 
    				&& conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
    			{
    				if (value == null) 
    				{ 
    					return default(T); 
    				}
    
    				conversionType = Nullable.GetUnderlyingType(conversionType);
    			}
    
    			return (T)Convert.ChangeType(value, conversionType);
    
    		}
    
    2. 方式二
    		public static object ChangeType(object value, Type conversionType)
    		{
    			// Note: This if block was taken from Convert.ChangeType as is, and is needed here since we're
    			// checking properties on conversionType below.
    			if (conversionType == null)
    			{
    				throw new ArgumentNullException("conversionType");
    			} // end if
    
    			// If it's not a nullable type, just pass through the parameters to Convert.ChangeType
    
    			if (conversionType.IsGenericType 
    				&& conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
    			{
    				// It's a nullable type, so instead of calling Convert.ChangeType directly which would throw a
    				// InvalidCastException (per http://weblogs.asp.net/pjohnson/archive/2006/02/07/437631.aspx),
    				// determine what the underlying type is
    				// If it's null, it won't convert to the underlying type, but that's fine since nulls don't really
    				// have a type--so just return null
    				// Note: We only do this check if we're converting to a nullable type, since doing it outside
    				// would diverge from Convert.ChangeType's behavior, which throws an InvalidCastException if
    				// value is null and conversionType is a value type.
    				if (value == null)
    				{
    					return null;
    				} // end if
    
    				// It's a nullable type, and not null, so that means it can be converted to its underlying type,
    				// so overwrite the passed-in conversion type with this underlying type
    				System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
    
    				conversionType = nullableConverter.UnderlyingType;
    			} // end if
    
    			// Now that we've guaranteed conversionType is something Convert.ChangeType can handle (i.e. not a
    			// nullable type), pass the call on to Convert.ChangeType
    			return Convert.ChangeType(value, conversionType);
    		}

    通过反射可以获得对象的所有属性以及类型,遍历所有属性调用上面的函数为对象赋值。

    public static void GetPost<T>(ref T t)
    			{
    				System.Collections.Specialized.NameValueCollection form = HttpContext.Current.Request.Form;
    
    				Type type = t.GetType();//获取类型
    				PropertyInfo[] pi = type.GetProperties();//获取属性集合
    				foreach (PropertyInfo p in pi)
    				{
    					if (form[p.Name] != null)
    					{
    						try
    						{
    							p.SetValue(t, ChangeType(form[p.Name], p.PropertyType), null);//为属性赋值,并转换键值的类型为该属性的类型
    						}
    						catch
    						{
    						}
    					}
    				}
    			}
    
  • 相关阅读:
    Python之字典
    Python之模块
    Python之字符串格式化
    结束循环
    循环
    C语言static和局部变量
    C static extern和全局变量
    extern static和函数
    C语言typedef
    C语言之文件包含
  • 原文地址:https://www.cnblogs.com/davinci/p/1652602.html
Copyright © 2011-2022 走看看