양식에서 솔리드 객체에 값 지정

3301 단어 대상
먼저 양식에서 얻은 문자열을 다음 코드를 사용하여 지정된 유형으로 변환해야 합니다.
object value = Convert.ChangeType(valueStr, desiredType);

//  valueStr   desiredType     

참고: Convert.ChangeType에서는 Nullable 유형의 값을 변환할 수 없습니다.변환하려면 다음 함수를 처리해야 합니다.
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

						{

						}

					}

				}

			}

좋은 웹페이지 즐겨찾기