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
    						{
    						}
    					}
    				}
    			}
    
  • 相关阅读:
    进程与线程
    silverlight中的几个冷门标记 {x:Null},d:DesignWidth,d:DesignHeight
    silverlight数据绑定模式TwoWay,OneWay,OneTime的研究
    silverlight 相册雏型
    IIS7的应用程序池
    silverlight如何在运行时用代码动态控制(或创建)动画
    庆祝silverlight 4 beta版发布,上几张养眼MM照片
    [转贴]Silverlight Socket 实现收发信息
    IIS7.5中神秘的ApplicationPoolIdentity
    silverlight中如何将string(字符串)写入Resource(资源)?
  • 原文地址:https://www.cnblogs.com/davinci/p/1652602.html
Copyright © 2011-2022 走看看