/// <summary> /// Transfers all field values from the inputObject to a new instance of the outputType /// using the FieldInfo as the common link. /// </summary> /// <param name="inputObject">The object containing values</param> /// <param name="outputype">The System.Type for the object to create.</param> /// <returns>The transferred output object</returns> public static object TransferLikeNamedFieldsToProperties(object inputObject, System.Type outputType) { //Get the Type of the Input Object Type InputType = inputObject.GetType(); //Retreive all the fields for the input object PropertyInfo[] InputObjectFieldInfoArray = InputType.GetProperties(); //Retreive the default Constructor for the Output object. This should be //the parameterless one. ConstructorInfo OutputObjectConstructorInfo = outputType.GetConstructor(System.Type.EmptyTypes); //Invoke the constructor to instance the output object object OutputObject = OutputObjectConstructorInfo.Invoke(null); //Loop through the properties on the input object and set the like named //field to the value of the property on the input object foreach (PropertyInfo CurrentPropertyInfo in InputObjectFieldInfoArray) { object FieldValue = CurrentPropertyInfo.GetValue(inputObject, null); PropertyInfo OutputPropertyInfo = OutputObject.GetType().GetProperty(CurrentPropertyInfo.Name); //Only set the value if the type on the input field is the same as the //property on the output object if (OutputPropertyInfo != null) { if (CurrentPropertyInfo.PropertyType == OutputPropertyInfo.PropertyType) { OutputPropertyInfo.SetValue(OutputObject, FieldValue, null); } } } return(OutputObject); }
/// <summary> /// Valueses the specified holder. /// </summary> /// <param name="holder">The holder.</param> /// <returns></returns> public IEnumerable <ActiveRecordBase> Values(object holder) { if (CurrentPropertyInfo != null) { foreach (object item in (System.Collections.IEnumerable)CurrentPropertyInfo.GetValue(holder, null)) { yield return((ActiveRecordBase)item); } } }
/// <summary> /// Transfers all property values from the inputObject to a new instance of the outputType /// using the PropertyName as the common link. /// </summary> /// <param name="inputObject">The object containing values</param> /// <param name="outputype">The System.Type for the object to be transferred to.</param> /// <param name="outputObject">The object assigned with the value of the input object</param> /// <returns></returns> public static object TransferLikeNamedProperties(object inputObject, System.Type outputType, object outputObject) { //Determine if a new object is returned or a reference object returned bool IsNewOutputObject = (outputObject == null?true:false); //Get the Type of the Input Object Type InputType = inputObject.GetType(); //Retreive all the properties for the input object PropertyInfo[] InputObjectPropertyInfoArray = InputType.GetProperties(); //create output object if it's not passed in if (IsNewOutputObject == true) { //Retreive the default Constructor for the Output object. This should be //the parameterless one. ConstructorInfo OutputObjectConstructorInfo = outputType.GetConstructor(System.Type.EmptyTypes); //Invoke the constructor to instance the output object outputObject = OutputObjectConstructorInfo.Invoke(null); } //Loop through the properties on the input object and set the like named //property to the value of the property on the input object foreach (PropertyInfo CurrentPropertyInfo in InputObjectPropertyInfoArray) { if (IsNewOutputObject == false && (CurrentPropertyInfo.Name.Equals("Code") | CurrentPropertyInfo.Name.Equals("RowIdentifier"))) { //for a referenced output object, do not assign code and rowidentifier column } else { object PropertyValue = CurrentPropertyInfo.GetValue(inputObject, null); PropertyInfo OutputPropertInfo = outputType.GetProperty(CurrentPropertyInfo.Name); //Only set the value if the type on the input property is the same as the //property on the output object if (OutputPropertInfo != null) { if (CurrentPropertyInfo.PropertyType == OutputPropertInfo.PropertyType) { OutputPropertInfo.SetValue(outputObject, PropertyValue, null); } } } } return(outputObject); }
/// <summary> /// Sets the value. /// </summary> /// <param name="item">The item.</param> /// <param name="value">The value.</param> public override void SetValue(ActiveRecordBase item, object value) { if (CurrentPropertyInfo == null) { return; } try { CurrentPropertyInfo.SetValue(item, Convert.ChangeType(value.ToString(), CurrentPropertyInfo.PropertyType), null); } catch { } }
/// <summary> /// Gets the value. /// </summary> /// <param name="item">The item.</param> /// <returns></returns> public override object GetValue(ActiveRecordBase item) { if (CurrentPropertyInfo == null) { return(null); } try { return(CurrentPropertyInfo.GetValue(item, null)); } catch { return(null); } }
/// <summary> /// Transfers all property values from the inputObject to a new instance of the outputType /// using the PropertyName as the common link. /// </summary> /// <param name="inputObject">The object containing values</param> /// <param name="outputype">The System.Type for the object to create.</param> /// <returns></returns> public static object TransferLikeNamedPropertiesToFields(object inputObject, System.Type outputType) { //Get the Type of the Input Object Type InputType = inputObject.GetType(); //Retreive all the properties for the input object PropertyInfo[] InputObjectPropertyInfoArray = InputType.GetProperties(); //Retreive the default Constructor for the Output object. This should be //the parameterless one. ConstructorInfo OutputObjectConstructorInfo = outputType.GetConstructor(System.Type.EmptyTypes); //Invoke the constructor to instance the output object object OutputObject = OutputObjectConstructorInfo.Invoke(null); //Loop through the properties on the input object and set the like named //field to the value of the property on the input object foreach (PropertyInfo CurrentPropertyInfo in InputObjectPropertyInfoArray) { object PropertyValue = CurrentPropertyInfo.GetValue(inputObject, null); PropertyInfo OutputFieldInfo = OutputObject.GetType().GetProperty(CurrentPropertyInfo.Name); //Only set the value if the type on the input property is the same as the //field on the output object if (OutputFieldInfo != null) { /* * if (CurrentPropertyInfo.PropertyType.ToString() == "System.Collections.ArrayList") * { * //get property values of arraylist * ArrayList PropertyValueArrayList = (ArrayList)PropertyValue; * * //loop through field list to find the field type same as property type * //and populate the field of that type. * FieldInfo[] OutputObjectFieldInfoArray = outputType.GetFields(); * foreach (FieldInfo CurrentFieldInfo in OutputObjectFieldInfoArray) * { * if (CurrentFieldInfo.FieldType == PropertyValueArrayList[0].GetType()) * { * ArrayList FieldValueArrayList = new ArrayList(); * foreach(object CurrentPropertyValue in PropertyValueArrayList) * { * FieldValueArrayList.Add(ObjectUtility.TransferLikeNamedProperties( * CurrentPropertyValue, CurrentFieldInfo.FieldType)); * } * OutputObject = FieldValueArrayList; * break; * } * } * } * else * {*/ if (CurrentPropertyInfo.PropertyType == OutputFieldInfo.PropertyType) { OutputFieldInfo.SetValue(OutputObject, PropertyValue, null); } //} } } return(OutputObject); }
/// <summary> /// Creates a projection of the specified source object where by default, all members are included if not explicitly excluded. /// </summary> private Hashtable CreateInclusiveProjection(object SourceObject) { // // CREATE THE HASH TABLE. // ALL PROJECTIONS ARE "OLD SCHOOL" HASH TABLES. // Hashtable CreatedProjection = new Hashtable(); // // GET THE TYPE OF THE ENTITY. // WE USE THIS TO REFLECT AGAINST THE SOURCE OBJECT SEVERAL TIMES. // Type SourceObjectType = SourceObject.GetType(); // // BY DEFAULT, THIS METHOD INCLUDES ALL MEMBERS UNLESS THEY WERE EXPLICITLY EXCLUDED. // // // PROCESS PROPERTIES. // foreach (PropertyInfo CurrentPropertyInfo in SourceObjectType.GetProperties()) { // // RENDER ALL PROPERTIES IF NOT OTHERWISE SPECIFIED. // if (!_ExcludedPropertyNames.ContainsKey(CurrentPropertyInfo.Name) && !_ExcludedMemberNames.ContainsKey(CurrentPropertyInfo.Name)) { // // THIS PROPERTY IS NOT EXCLUDED - SO INCLUDE IT IN THE PROJECTION. // object CurrentValue = CurrentPropertyInfo.GetValue(SourceObject, null); // // IF THE VALUE IS NOT NULL, THEN INCLUDE IT. // IF THE VALUE IS NULL BUT WE ARE SUPPOSED TO INCLUDE NULL VALUES THEN ALSO INCLUDE IT. // if (CurrentValue != null || _NullValueBehavior == NullValueBehavior.IncludeNulls) { // // WE ARE INCLUDING THIS VALUE IN THE PROJECTION. // if (_MemberRenameMap.ContainsKey(CurrentPropertyInfo.Name)) { CreatedProjection[_MemberRenameMap[CurrentPropertyInfo.Name]] = CurrentValue; } else { CreatedProjection[CurrentPropertyInfo.Name] = CurrentValue; } } } } // // PROCESS FIELDS. // foreach (FieldInfo CurrentFieldInfo in SourceObjectType.GetFields()) { // // INCLUDE ALL FIELDS UNLESS EXPLICITLY EXCLUDED. // if (!_ExcludedFieldNames.ContainsKey(CurrentFieldInfo.Name) && !_ExcludedMemberNames.ContainsKey(CurrentFieldInfo.Name)) { // // THIS FIELD IS NOT EXCLUDED - SO INCLUDE IT IN THE PROJECTION. // object CurrentValue = CurrentFieldInfo.GetValue(SourceObject); // // IF THE VALUE IS NOT NULL, THEN INCLUDE IT. // IF THE VALUE IS NULL BUT WE ARE SUPPOSED TO INCLUDE NULL VALUES THEN ALSO INCLUDE IT. // if (CurrentValue != null || _NullValueBehavior == NullValueBehavior.IncludeNulls) { // // WE ARE INCLUDING THIS VALUE IN THE PROJECTION. // RENAME AS SPECIFIED. // if (_MemberRenameMap.ContainsKey(CurrentFieldInfo.Name)) { CreatedProjection[_MemberRenameMap[CurrentFieldInfo.Name]] = CurrentValue; } else { CreatedProjection[CurrentFieldInfo.Name] = CurrentValue; } } } } return(CreatedProjection); }