public static string ExecuteCallbackMethod(Control control, string callbackArgument) { Type controlType = control.GetType(); // Deserialize the callback JSON into CLR objects JavaScriptSerializer js = new JavaScriptSerializer(); Dictionary <string, object> callInfo = js.DeserializeObject(callbackArgument) as Dictionary <string, object>; // Get the call information string methodName = (string)callInfo["name"]; object[] args = (object[])callInfo["args"]; string clientState = (string)callInfo["state"]; // Attempt to load the client state IClientStateManager csm = control as IClientStateManager; if (csm != null && csm.SupportsClientState) { csm.LoadClientState(clientState); } // call the method object result = null; string error = null; try { // Find a matching static or instance method. Only public methods can be invoked MethodInfo mi = controlType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); if (mi == null) { throw new MissingMethodException(controlType.FullName, methodName); } // Verify that the method has the corrent number of parameters as well as the ExtenderControlMethodAttribute ParameterInfo[] methodParams = mi.GetParameters(); ExtenderControlMethodAttribute methAttr = (ExtenderControlMethodAttribute) Attribute.GetCustomAttribute(mi, typeof(ExtenderControlMethodAttribute)); if (methAttr == null || !methAttr.IsScriptMethod || args.Length != methodParams.Length) { throw new MissingMethodException(controlType.FullName, methodName); } // Convert each argument to the parameter type if possible // NOTE: I'd rather have the ObjectConverter from within System.Web.Script.Serialization namespace for this object[] targetArgs = new object[args.Length]; for (int i = 0; i < targetArgs.Length; i++) { if (args[i] == null) { continue; } targetArgs[i] = Convert.ChangeType(args[i], methodParams[i].ParameterType, CultureInfo.InvariantCulture); } result = mi.Invoke(control, targetArgs); } catch (Exception ex) { // Catch the exception information to relay back to the client if (ex is TargetInvocationException) { ex = ex.InnerException; } error = ex.GetType().FullName + ":" + ex.Message; } // return the result Dictionary <string, object> resultInfo = new Dictionary <string, object>(); if (error == null) { resultInfo["result"] = result; if (csm != null && csm.SupportsClientState) { resultInfo["state"] = csm.SaveClientState(); } } else { resultInfo["error"] = error; } // Serialize the result info into JSON return(js.Serialize(resultInfo)); }
public static void DescribeComponent(object instance, ScriptComponentDescriptor descriptor, IUrlResolutionService urlResolver, IControlResolver controlResolver) { // validate preconditions if (instance == null) { throw new ArgumentNullException("instance"); } if (descriptor == null) { throw new ArgumentNullException("descriptor"); } if (urlResolver == null) { urlResolver = instance as IUrlResolutionService; } if (controlResolver == null) { controlResolver = instance as IControlResolver; } // describe properties PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance); foreach (PropertyDescriptor prop in properties) { ExtenderControlPropertyAttribute propAttr = null; ExtenderControlEventAttribute eventAttr = null; string propertyName = prop.Name; // Try getting a property attribute propAttr = (ExtenderControlPropertyAttribute)prop.Attributes[typeof(ExtenderControlPropertyAttribute)]; if (propAttr == null || !propAttr.IsScriptProperty) { // Try getting an event attribute eventAttr = (ExtenderControlEventAttribute)prop.Attributes[typeof(ExtenderControlEventAttribute)]; if (eventAttr == null || !eventAttr.IsScriptEvent) { continue; } } // attempt to rename the property/event ClientPropertyNameAttribute nameAttr = (ClientPropertyNameAttribute)prop.Attributes[typeof(ClientPropertyNameAttribute)]; if (!string.IsNullOrEmpty(nameAttr.PropertyName)) { propertyName = nameAttr.PropertyName; } object value = prop.GetValue(instance); if (value == null) { continue; } // determine whether to serialize the value of a property. readOnly properties should always be serialized bool serialize = prop.ShouldSerializeValue(instance) || prop.IsReadOnly; if (serialize) { // get the value of the property, skip if it is null Control c = null; // convert and resolve the value if (eventAttr != null && prop.PropertyType != typeof(String)) { throw new InvalidOperationException( "ExtenderControlEventAttribute can only be applied to a property with a PropertyType of System.String."); } else { if (!prop.PropertyType.IsPrimitive && !prop.PropertyType.IsEnum) { // Check if we can use any of our custom converters // (first do a direct lookup on the property type, // but also check all of its base types if nothing // was found) Converter <object, string> customConverter = null; if (!_customConverters.TryGetValue(prop.PropertyType, out customConverter)) { foreach (KeyValuePair <Type, Converter <object, string> > pair in _customConverters) { if (prop.PropertyType.IsSubclassOf(pair.Key)) { customConverter = pair.Value; break; } } } // Use the custom converter if found, otherwise use // its current type converter if (customConverter != null) { value = customConverter(value); } else { // Determine if we should let ASP.NET AJAX handle this type of conversion, as it supports JSON serialization if (propAttr != null && propAttr.UseJsonSerialization) { if (value is IEnumerable) { List <object> lst = new List <object>(); foreach (object a in (IEnumerable)value) { object b; Type typ = a.GetType(); if (a as IUICollectionItem != null) { b = a is UriValue ? urlResolver.ResolveClientUrl(((UriValue)a).Value.ToString()) : ((IUICollectionItem)a).Value; } else if (typ.IsPrimitive || typ.IsValueType) { b = a; } else { b = BuildGraph(a, urlResolver, controlResolver); } if (b != null) { lst.Add(b); } } if (lst.Count > 0) { value = lst; } } else if (value.GetType().IsPrimitive || value.GetType().IsValueType) { } else { value = BuildGraph(value, urlResolver, controlResolver); } // Use ASP.NET JSON serialization } else { // Use the property's own converter TypeConverter conv = prop.Converter; value = conv.ConvertToString(null, CultureInfo.InvariantCulture, value); } } } if (prop.Attributes[typeof(IDReferencePropertyAttribute)] != null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (prop.Attributes[typeof(UrlPropertyAttribute)] != null && urlResolver != null) { value = urlResolver.ResolveClientUrl((string)value); } } // add the value as an appropriate description if (eventAttr != null) { descriptor.AddEvent(propertyName, (string)value); } else if (prop.Attributes[typeof(ElementReferenceAttribute)] != null) { if (c == null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (c != null) { value = c.ClientID; } descriptor.AddElementProperty(propertyName, (string)value); } else if (prop.Attributes[typeof(ComponentReferenceAttribute)] != null) { if (c == null && controlResolver != null) { c = controlResolver.ResolveControl((string)value); } if (c != null) { ExtenderControlBase ex = c as ExtenderControlBase; if (ex != null && ex.BehaviorID.Length > 0) { value = ex.BehaviorID; } else { value = c.ClientID; } } descriptor.AddComponentProperty(propertyName, (string)value); } else { if (c != null) { value = c.ClientID; } descriptor.AddProperty(propertyName, value); } } } // determine if we should describe methods foreach ( MethodInfo method in instance.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)) { ExtenderControlMethodAttribute methAttr = (ExtenderControlMethodAttribute) Attribute.GetCustomAttribute(method, typeof(ExtenderControlMethodAttribute)); if (methAttr == null || !methAttr.IsScriptMethod) { continue; } // We only need to support emitting the callback target and registering the WebForms.js script if there is at least one valid method Control control = instance as Control; if (control != null) { // Force WebForms.js control.Page.ClientScript.GetCallbackEventReference(control, null, null, null); // Add the callback target descriptor.AddProperty("_callbackTarget", control.UniqueID); } break; } }
private static Dictionary <string, object> BuildGraph(object obj, IUrlResolutionService uriResolver, IControlResolver controlResolver) { Dictionary <string, object> dict = new Dictionary <string, object>(); PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(obj); foreach (PropertyDescriptor pd in pdc) { ExtenderControlPropertyAttribute ecpa = GetAttribute <ExtenderControlPropertyAttribute>(pd); ClientPropertyNameAttribute cpna = GetAttribute <ClientPropertyNameAttribute>(pd); IDReferencePropertyAttribute idr = GetAttribute <IDReferencePropertyAttribute>(pd); UrlPropertyAttribute ura = GetAttribute <UrlPropertyAttribute>(pd); ExtenderControlMethodAttribute ecma = GetAttribute <ExtenderControlMethodAttribute>(pd); ExtenderControlEventAttribute ecea = GetAttribute <ExtenderControlEventAttribute>(pd); ElementReferenceAttribute era = GetAttribute <ElementReferenceAttribute>(pd); ComponentReferenceAttribute cra = GetAttribute <ComponentReferenceAttribute>(pd); if (ecpa == null && cra == null && cpna == null && era == null) { continue; } string propName = cpna != null && !string.IsNullOrEmpty(cpna.PropertyName) ? cpna.PropertyName : pd.Name; if (propName == "ClientClassName") { propName = "typeToBuild"; } object o = null; object value = pd.GetValue(obj); if (value == null) { continue; } if (value as IClientClass != null && ((IClientClass)value).NotSet) { continue; } if (value as ClientEvalScript != null && string.IsNullOrEmpty((value as ClientEvalScript).ClientScript)) { continue; } if (pd.PropertyType == typeof(string)) { o = value; if (ura != null) { o = uriResolver.ResolveClientUrl((string)o); } else if (idr != null || cra != null) { o = BuildGraph(new Reference { ReferenceType = ReferenceType.Component, ClientId = controlResolver.ResolveControl((string)o).ClientID }, uriResolver, controlResolver); } else if (era != null) { Control c = controlResolver.ResolveControl((string)o); o = BuildGraph( new Reference { ReferenceType = ReferenceType.Element, ClientId = c == null ? (string)o : c.ClientID }, uriResolver, controlResolver); } } else if (value is IEnumerable) { List <object> lst = new List <object>(); foreach (object a in (IEnumerable)value) { object b; if (a as IUICollectionItem != null) { b = a is UriValue ? uriResolver.ResolveClientUrl(((UriValue)a).Value.ToString()) : ((IUICollectionItem)a).Value; } else { b = BuildGraph(a, uriResolver, controlResolver); } if (b != null) { lst.Add(b); } } if (lst.Count > 0) { o = lst; } } else if (pd.PropertyType.IsEnum) { o = Enum.GetName(pd.PropertyType, value); } else if (pd.PropertyType.IsPrimitive || pd.PropertyType.IsValueType) { o = value; } else { o = BuildGraph(value, uriResolver, controlResolver); } if (o != null) { dict.Add(propName, o); } } return(dict.Count > 0 ? dict : null); }