/// <summary> /// Method to set an object property value. /// </summary> /// <param name="obj">The object to set property value.</param> /// <param name="propertyName">The property name.</param> /// <param name="value">The property value.</param> /// <exception cref="InvalidOperationException">Occurs if the property name is not found or if the property is as read only.</exception> public static object SetPropertyValue(this object obj, string propertyName, object value) { try { object[] values = new object[] { value }; PropertyInfo prop = obj?.GetType()? .GetProperty ( propertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance ); if (prop != null && prop.CanWrite) { prop.SetValue(obj, value, null); } else { log.Error(prop.ToString()); throw new InvalidOperationException("Object property does not exists or property is lock to write."); } } catch (Exception ex) { log.Error(ex.Output(), ex); throw; } return(value); }
public OutputSettingsException(PropertyInfo setting, object reqValue, object capValue) : base("Failed to create output setting for '" + setting.ToString() + "'") { this.setting = setting; this.reqValue = reqValue; this.capValue = capValue; }
internal PropertyMetadata(PropertyInfo info) { PropertyInfo = info; #if !DataAnnotations_Missing Validators = ImmutableArray.CreateRange(info.GetCustomAttributes(typeof(ValidationAttribute), true).OfType<ValidationAttribute>()); #endif IsIndexed = info.GetIndexParameters().Length > 0; m_GetMethod = PropertyInfo.GetMethod; m_SetMethod = PropertyInfo.SetMethod; PropertyType = info.PropertyType; var name = info.ToString(); Name = name.Substring(name.IndexOf(" ", StringComparison.Ordinal) + 1); if (IsIndexed) PropertyChangedEventArgs = new PropertyChangedEventArgs(info.Name + "[]"); else PropertyChangedEventArgs = new PropertyChangedEventArgs(info.Name); #if !DataAnnotations_Missing IsKey = info.GetCustomAttributes(typeof(KeyAttribute), true).Any(); var doNotMap = info.GetCustomAttributes(typeof(NotMappedAttribute), true).Any(); if (!doNotMap) { var column = (ColumnAttribute)info.GetCustomAttributes(typeof(ColumnAttribute), true).SingleOrDefault(); MappedColumnName = column != null ? column.Name : Name; } var decomposeAttribute = (DecomposeAttribute)(info.GetCustomAttributes(typeof(DecomposeAttribute), true).FirstOrDefault()); if (decomposeAttribute != null) { Decompose = true; DecompositionPrefix = decomposeAttribute.Prefix; } IgnoreOnInsert = info.GetCustomAttributes(typeof(IgnoreOnInsertAttribute), true).Any(); IgnoreOnUpdate = info.GetCustomAttributes(typeof(IgnoreOnUpdateAttribute), true).Any(); #endif }
private void ProcessSingleObject(EntityContextInfo entityInfo, ElasticsearchCrudJsonWriter elasticsearchCrudJsonWriter, PropertyInfo prop, bool createPropertyMappings) { TraceProvider.Trace(TraceEventType.Verbose, "ElasticsearchMapping: Property is an Object: {0}", prop.ToString()); // This is a single object and not a reference to it's parent if (createPropertyMappings && prop.GetValue(entityInfo.Document) == null) { prop.SetValue(entityInfo.Document,Activator.CreateInstance(prop.PropertyType)); } if (prop.GetValue(entityInfo.Document) != null && SaveChildObjectsAsWellAsParent) { var child = GetDocumentType(prop.GetValue(entityInfo.Document).GetType()); var parent = GetDocumentType(entityInfo.EntityType); if (!SerializedTypes.Contains(child + parent)) { SerializedTypes.Add(parent + child); if (ProcessChildDocumentsAsSeparateChildIndex) { ProcessSingleObjectAsChildDocument(entityInfo, elasticsearchCrudJsonWriter, prop, createPropertyMappings); } else { ProcessSingleObjectAsNestedObject(entityInfo, elasticsearchCrudJsonWriter, prop, createPropertyMappings); } } } }
public static void GetSerializationInfo(SerializationInfo info, PropertyInfo p) { GetSerializationInfo(info, p.Name, p.ReflectedType, p.ToString(), p.SerializationToString(), MemberTypes.Property, genericArguments: null); }
public ValidationException(ValidationConstraint constraint, PropertyInfo prop) : base(prop.ToString()) { ValidationConstraint = constraint; Property = prop; }
void ProcessProperty(PropertyInfo propertyInfo) { Debug.WriteLine ("Add property " + propertyInfo.ToString ()); }
public static string ValueToString( object obj, PropertyInfo prop ) { try { if (prop == null) return "!Null PropertyInfo!"; if (obj == null) { MethodInfo mi = prop.GetGetMethod(false); if (mi == null) // don't expect this to *ever* get hit.. would only occur on set-only props return "!Null Obj, MethodInfo!"; // or non-public ones if (!mi.IsStatic) return "-null-"; // okay, wtf is going on! throw new ApplicationException(String.Format("{0} was null", prop.ToString())); } return ValueToString( prop.GetValue( obj, null ) ); } catch ( Exception e ) { LogHelper.LogException(e); return String.Format( "!{0}!", e.GetType() ); } }
public List <string> FieldsRequired(Object obj, List <string> errorFieldsRequired) { List <string> results = new List <string>(); try { var properties = GetProperties(obj); /* * foreach (PropertyInfo field in properties) * { * * string e = field.PropertyType.Name; * if (!(e.Equals("String"))) * { * * System.Reflection.PropertyInfo pi = obj.GetType().GetProperty(e); * object value = (object)(pi.GetValue(obj, null)); * * List<string> results2 = FieldsRequired(value, errorFieldsRequired); * foreach (var item in results2) * { * results.Add(item); * } * } * } */ foreach (var field in errorFieldsRequired) { foreach (var p in properties) { if (p.Name.Equals(field)) { System.Reflection.PropertyInfo pi = obj.GetType().GetProperty(p.Name); var epp = pi.ToString(); if (epp.Contains("System.Decimal")) { decimal dec = (decimal)(pi.GetValue(obj, null)); if (decimal.Equals(decimal.Zero, dec)) { results.Add(p.Name); } } else if (epp.Contains("Int32")) { int value = (int)(pi.GetValue(obj, null)); if (value == 0) { results.Add(p.Name); } } else if (epp.Contains("Boolean")) { } else { String value = (String)(pi.GetValue(obj, null)); if (string.IsNullOrEmpty(value)) { results.Add(p.Name); } } } } } } catch (Exception ex) { throw new System.ArgumentException("Error on function FieldsRequired. Error: ", errorMaxLength(ex.Message)); } return(results); }
// CreateSetDelegate public static SetHandler CreateSetHandler(Type type, PropertyInfo propertyInfo) { string _key = string.Format("{0}.{1}", propertyInfo.DeclaringType, propertyInfo.ToString()); if (s_SetHandlerCache.ContainsKey(_key)) { return s_SetHandlerCache[_key] as SetHandler; } MethodInfo setMethodInfo = propertyInfo.GetSetMethod(true); DynamicMethod dynamicSet = CreateSetDynamicMethod(type); ILGenerator setGenerator = dynamicSet.GetILGenerator(); setGenerator.Emit(OpCodes.Ldarg_0); setGenerator.Emit(OpCodes.Ldarg_1); UnboxIfNeeded(setMethodInfo.GetParameters()[0].ParameterType, setGenerator); setGenerator.Emit(OpCodes.Call, setMethodInfo); setGenerator.Emit(OpCodes.Ret); SetHandler _newHandler = (SetHandler)dynamicSet.CreateDelegate(typeof(SetHandler)); s_SetHandlerCache.Add(_key, _newHandler); return _newHandler; }