9using System.Reflection;
11using System.ComponentModel;
20 public static class Utilities
22 #region Replacements for VB runtime functionality
28 public static bool IsNumeric(
object value)
31 return double.TryParse(value.ToString(), System.Globalization.NumberStyles.Any,
32 System.Globalization.NumberFormatInfo.InvariantInfo, out dbl);
44 public static object CallByName(
45 object target,
string methodName,
CallType callType,
52 return MethodCaller.CallPropertyGetter(target, methodName);
57 MethodCaller.CallPropertySetter(target, methodName, args[0]);
62 return MethodCaller.CallMethod(target, methodName, args);
76 public static Type GetPropertyType(Type propertyType)
78 Type type = propertyType;
79 if (type.IsGenericType &&
80 (type.GetGenericTypeDefinition() == typeof(Nullable<>)))
81 return Nullable.GetUnderlyingType(type);
90 public static Type GetChildItemType(Type listType)
94 result = listType.GetElementType();
98 (DefaultMemberAttribute)Attribute.GetCustomAttribute(
99 listType, typeof(DefaultMemberAttribute));
102 foreach (PropertyInfo prop
in listType.GetProperties(
103 BindingFlags.Public |
104 BindingFlags.Instance |
105 BindingFlags.FlattenHierarchy))
107 if (prop.Name == indexer.MemberName)
108 result = Utilities.GetPropertyType(prop.PropertyType);
113 result = listType.GetMethod(
"get_Item")?.ReturnType;
153 public static object CoerceValue(Type desiredType, Type valueType,
object oldValue,
object value)
155 if (desiredType.IsAssignableFrom(valueType))
162 if (desiredType.IsGenericType)
164 if (desiredType.GetGenericTypeDefinition() == typeof(Nullable<>))
167 else if (valueType.Equals(typeof(
string)) && System.Convert.ToString(value) ==
string.Empty)
170 desiredType = Utilities.GetPropertyType(desiredType);
173 if (desiredType.IsEnum)
175 if (value is
byte? && ((
byte?) value).HasValue)
176 return System.Enum.Parse(desiredType, ((
byte?) value).Value.ToString());
177 if (value is
short? && ((
short?) value).HasValue)
178 return System.Enum.Parse(desiredType, ((
short?) value).Value.ToString());
179 if (value is
int? && ((
int?) value).HasValue)
180 return System.Enum.Parse(desiredType, ((
int?) value).Value.ToString());
181 if (value is
long? && ((
long?) value).HasValue)
182 return System.Enum.Parse(desiredType, ((
long?) value).Value.ToString());
185 if (desiredType.IsEnum &&
186 (valueType.Equals(typeof(
string)) || Enum.GetUnderlyingType(desiredType).Equals(valueType)))
187 return System.Enum.Parse(desiredType, value.ToString());
189 if (desiredType.Equals(typeof(SmartDate)) && oldValue !=
null)
192 value =
string.Empty;
193 var tmp = (SmartDate)oldValue;
194 if (valueType.Equals(typeof(DateTime)))
195 tmp.Date = (DateTime)value;
197 tmp.Text = value.ToString();
201 if ((desiredType.IsPrimitive || desiredType.Equals(typeof(decimal))) &&
202 valueType.Equals(typeof(
string)) &&
string.IsNullOrEmpty((
string)value))
207 if (desiredType.Equals(typeof(
string)) && value !=
null)
209 return value.ToString();
212 return Convert.ChangeType(value, desiredType);
216 TypeConverter cnv = TypeDescriptor.GetConverter(desiredType);
217 TypeConverter cnv1 = TypeDescriptor.GetConverter(valueType);
218 if (cnv !=
null && cnv.CanConvertFrom(valueType))
219 return cnv.ConvertFrom(value);
220 else if (cnv1 !=
null && cnv1.CanConvertTo(desiredType))
221 return cnv1.ConvertTo(value, desiredType);
259 public static D CoerceValue<D>(Type valueType,
object oldValue,
object value)
261 return (D)(CoerceValue(typeof(D), valueType, oldValue, value));
266 internal static void ThrowIfAsyncMethodOnSyncClient(
bool isSync, System.Reflection.MethodInfo method)
269 && ApplicationContext.ExecutionLocation != ApplicationContext.ExecutionLocations.Server
270 && MethodCaller.IsAsyncMethod(method))
283 internal static void ThrowIfAsyncMethodOnSyncClient(
bool isSync,
object obj,
string methodName)
286 && ApplicationContext.ExecutionLocation != ApplicationContext.ExecutionLocations.Server
287 && MethodCaller.IsAsyncMethod(obj, methodName))
302 internal static void ThrowIfAsyncMethodOnSyncClient(
bool isSync,
object obj,
string methodName, params
object[] parameters)
305 && ApplicationContext.ExecutionLocation != ApplicationContext.ExecutionLocations.Server
306 && MethodCaller.IsAsyncMethod(obj, methodName, parameters))
A strongly-typed resource class, for looking up localized strings, etc.
static string AsyncMethodOnSyncClientNotAllowed
Looks up a localized string similar to Cannot synchronously call {0} which is an asynchronous method.
CallType
Valid options for calling a property or method via the Csla.Utilities.CallByName method.
@ Method
Invokes a method.
@ Set
Sets a value into a property.
@ Get
Gets a value from a property.
@ Let
Sets a value into a property.