private bool GetBool(string condition) { Type classType = TargetObject.GetType(); SerializedProperty property = SerializedObject.FindProperty(condition); if (property != null) { return(GetBool(property)); } MethodInfo method = classType.GetMethod(condition, findValueFlags); if (method != null) { object methodValue = method.Invoke(TargetObject, null); return(methodValue.GetBool()); } FieldInfo field = classType.GetField(condition, findValueFlags); if (field != null) { object fieldValue = field.GetValue(TargetObject); return(fieldValue.GetBool()); } Debug.LogError($"Your condition in InspectorHideAttribute is invalid! Class: {classType}"); return(true); }
public void OnAfterDeserialize() { argumentList = new object[arguments.Length]; var paramTypeList = new Type[arguments.Length]; for (int i = 0; i < argumentList.Length; i++) { argumentList[i] = arguments[i].UnpackParameter(); paramTypeList[i] = arguments[i].ArgumentType; } methodDeleted = false; try { if (target.IsMethod) { methodInvoker = TargetObject.GetType().DelegateForInvoke(Target.TargetMethod.MethodName, paramTypeList); } else { fieldSetter = TargetObject.GetType().DelegateForSetField(Target.TargetFieldSetter.FieldName); } } catch (MissingMethodException) { methodDeleted = true; } catch (MissingFieldException) { methodDeleted = true; } }
public override string ToString() { return(string.Format("NodeTranslatorBound {0} {1} for {2}", Priority, TargetObject.GetType().ExcName(), Method.GetParameters()[1].ParameterType.ExcName())); }
void GetObject() { var ps = TargetObject.GetType().GetProperties(); _resultObj = Activator.CreateInstance(TargetObject.GetType()); foreach (var p in ps) { if (p.CanWrite) { p.SetValue(_resultObj, p.GetValue(TargetObject, null), null); } if (p.PropertyType != typeof(string)) { continue; } var ctrl = flowLayoutPanel1.Controls.Find(p.Name, true); if (ctrl.Any()) { var value = ctrl[0].Text; if (!string.IsNullOrEmpty(value) && value != DefaultEmptyString) { p.SetValue(_resultObj, value, null); } } } }
protected override void Invoke(object parameter) { if (base.AssociatedObject != null) { MethodDescriptor methodDescriptor = FindBestMethod(parameter); if (methodDescriptor != null) { ParameterInfo[] parameters = methodDescriptor.Parameters; if (parameters.Length == 0) { methodDescriptor.MethodInfo.Invoke(Target, null); } else if (parameters.Length == 2 && base.AssociatedObject != null && parameter != null && parameters[0].ParameterType.IsAssignableFrom(base.AssociatedObject.GetType()) && parameters[1].ParameterType.IsAssignableFrom(parameter.GetType())) { methodDescriptor.MethodInfo.Invoke(Target, new object[2] { base.AssociatedObject, parameter }); } } else if (TargetObject != null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "no ay effect", new object[2] { MethodName, TargetObject.GetType().Name })); } } }
protected override void OnTargetChanged() { if (TargetObject != null && TargetProperty != null) { var property = TargetObject.GetType().GetProperty(TargetProperty); if (property == null) { throw new Exception(string.Format("Property {0} on target type {1} not found", TargetProperty, TargetObject.GetType().FullName)); } if (property.PropertyType != typeof(Rectangle)) { throw new Exception(string.Format("Expecting TargetProperty {0} on target type {1} to be of type System.Drawing.Rectangle, but found it to be of type {2}", TargetProperty, TargetObject.GetType().FullName, property.PropertyType.FullName)); } var setter = property.GetSetMethod(); if (setter == null) { throw new Exception(string.Format("Expecting TargetProperty {0} on target type {1} to be settable, but it is not.", TargetProperty, TargetObject.GetType().FullName)); } var argument = Expression.Parameter(property.PropertyType, property.Name); var instance = Expression.Parameter(typeof(Shape), "this"); var typed = Expression.Convert(instance, TargetObject.GetType()); var call = Expression.Call(typed, setter, argument); this._lambda = Expression.Lambda <System.Action <Shape, Rectangle> >(call, instance, argument).Compile(); Debug.WriteLine("Lambda created"); } else { this._lambda = null; } }
public object Invoke(object[] args) { Type[] argTypes = new Type[args.Length]; for (int i = 0; i < argTypes.Length; i++) { argTypes[i] = args[i] != null ? args[i].GetType() : typeof(object); } // strict matching first MethodInfo targetMethodInfo = FindMethod(argTypes); // fuzzy matching if (targetMethodInfo == null) { MethodInfo[] methods = GetAllMethods(); if (log.IsEnabledFor(LogEvent.Debug)) { log.Write(LogEvent.Debug, "Invoke '{0}': Found {1} methods for matching", MethodName, methods.Length); } for (int i = 0; i < methods.Length; i++) { if (methods[i].Name == MethodName && methods[i].GetParameters().Length == args.Length && CheckParamsCompatibility(methods[i].GetParameters(), argTypes, args)) { targetMethodInfo = methods[i]; break; } } } if (targetMethodInfo == null) { string[] argTypeNames = new string[argTypes.Length]; for (int i = 0; i < argTypeNames.Length; i++) { argTypeNames[i] = argTypes[i].Name; } string argTypeNamesStr = String.Join(",", argTypeNames); throw new MissingMethodException( (TargetObject is Type ? (Type)TargetObject : TargetObject.GetType()).FullName, MethodName); } object[] argValues = PrepareActualValues(targetMethodInfo.GetParameters(), args); object res = null; try { res = targetMethodInfo.Invoke(TargetObject is Type ? null : TargetObject, argValues); } catch (TargetInvocationException tiEx) { if (tiEx.InnerException != null) { throw new Exception(tiEx.InnerException.Message, tiEx.InnerException); } else { throw; } } return(res); }
protected MethodInfo[] GetAllMethods() { if (TargetObject is Type) { return(((Type)TargetObject).GetMethods(BindingFlags.Static | BindingFlags.Public)); } return(TargetObject.GetType().GetMethods()); }
/// <summary> /// 调用目标对象的方法,开始工作。调用目标对象方法前,监听目标对象的工作完成事件。 /// 目标对象工作完成后,要删除掉该监听器,以保证只在这个动作调用目标对象方法工作期间进行监听。 /// </summary> public override void Invoke() { //调用前监听对象异步工作完成事件 TargetObject.Completed += TargetObjectCompleted; //调用对象方法 IsBusy = true; State = State.Start; TargetObject.GetType().GetMethod(MethodName).Invoke(TargetObject, null); }
protected MethodInfo FindMethod(Type[] argTypes) { if (TargetObject is Type) { // static method return(((Type)TargetObject).GetMethod(MethodName, BindingFlags.Static | BindingFlags.Public, null, argTypes, null)); } return(TargetObject.GetType().GetMethod(MethodName, argTypes)); }
public override string ToString() { if (TargetObject == null) { return("NoTarget"); } string method = TargetMethod == "" ? "NoTargetMethod" : TargetMethod; return(TargetObject.GetType().Name + "." + method); }
public object Invoke(object[] args) { Type[] argTypes = new Type[args.Length]; for (int i = 0; i < argTypes.Length; i++) { argTypes[i] = args[i] != null ? args[i].GetType() : typeof(object); } // strict matching first MethodInfo targetMethodInfo = FindMethod(argTypes); // fuzzy matching if (targetMethodInfo == null) { var methods = GetAllMethods(); foreach (var m in methods) { if (m.Name == MethodName && m.GetParameters().Length == args.Length && CheckParamsCompatibility(m.GetParameters(), argTypes, args)) { targetMethodInfo = m; break; } } } if (targetMethodInfo == null) { string[] argTypeNames = new string[argTypes.Length]; for (int i = 0; i < argTypeNames.Length; i++) { argTypeNames[i] = argTypes[i].Name; } string argTypeNamesStr = String.Join(",", argTypeNames); throw new MissingMemberException( (TargetObject is Type ? (Type)TargetObject : TargetObject.GetType()).FullName + "." + MethodName); } object[] argValues = PrepareActualValues(targetMethodInfo.GetParameters(), args); object res = null; try { res = targetMethodInfo.Invoke(TargetObject is Type ? null : TargetObject, argValues); } catch (TargetInvocationException tiEx) { if (tiEx.InnerException != null) { throw new Exception(tiEx.InnerException.Message, tiEx.InnerException); } else { throw; } } return(res); }
protected override DependencyPropertyProxy GetTargetProxy() { EventInfo updateEvent = TargetObject.GetType().GetEvent(Expression.UpdateEvent, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); DependencyPropertyProxy proxy = new DependencyPropertyProxy(TargetObject, TargetProperty, updateEvent); proxy.OnPropertyChanged += ProxyOnOnPropertyChanged; proxy.Attach(); return(proxy); }
private void OnEvent(object sender, EventArgs e) { if ((TargetObject == null) || string.IsNullOrEmpty(MethodName)) { return; } var methodInfo = TargetObject.GetType().GetRuntimeMethod(MethodName, EmptyTypes); methodInfo.Invoke(TargetObject, null); }
public Type GetObjectType() { Type targetType = TargetObject.GetType(); System.Reflection.PropertyInfo pInfo = targetType.GetProperty(TargetProperty, BindingFlags.Instance | BindingFlags.Public); if (pInfo == null) { throw new MissingMemberException(targetType.ToString(), TargetProperty); } return(pInfo.PropertyType); }
/// <summary> /// Invoked by an <see cref="Spring.Objects.Factory.IObjectFactory"/> /// after it has set all object properties supplied /// (and satisfied <see cref="Spring.Objects.Factory.IObjectFactoryAware"/> /// and ApplicationContextAware). /// </summary> /// <exception cref="System.Exception"> /// In the event of misconfiguration (such as failure to set an essential /// property) or if initialization fails. /// </exception> public override void AfterPropertiesSet() { if (TargetType == null && TargetObject == null) { throw new ArgumentException("One of the TargetType or TargetObject properties must be set."); } if (TargetProperty == null) { throw new ArgumentException("The TargetProperty property is required."); } Type targetType = null; BindingFlags propertyFlags = BindingFlags.Public | BindingFlags.IgnoreCase; if (TargetObject == null) { // a static property... propertyFlags |= BindingFlags.Static; targetType = TargetType; if (TargetProperty.IndexOf(".") == -1) { Property = targetType.GetProperty(TargetProperty, propertyFlags); } else { // $?#@! a nested static property... recurse to the end property string property = TargetProperty; int propertyIndex = property.IndexOf("."); string startProperty = property.Substring(0, propertyIndex); Property = targetType.GetProperty(startProperty, propertyFlags); TargetObject = Property.GetValue(null, new object[] {}); TargetProperty = property.Substring(propertyIndex + 1); AfterPropertiesSet(); } } else { // an instance property... propertyFlags |= BindingFlags.Instance; targetType = TargetObject.GetType(); // using the object wrapper does nested property lookup Property = _targetObjectWrapper.GetPropertyInfo(TargetProperty); } if (Property == null) { throw new InvalidPropertyException(targetType, TargetProperty); } if (!Property.CanRead) { throw new NotWritablePropertyException(TargetProperty, targetType); } base.AfterPropertiesSet(); }
/// <inheritdoc /> protected override void Invoke(object parameter) { if (TargetObject != null && MethodName != null) { var method = TargetObject.GetType().GetMethod(MethodName); if (method != null) { method.Invoke(TargetObject, null); } } }
public override void Initialize() { TargetProperty = TargetObject.GetType().GetProperty(Expression.TargetField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (TargetProperty == null) { throw new Exception("PropertyBinding : no property named " + Expression.TargetField + " in object of type " + TargetObject.GetType()); } TargetProxy = GetTargetProxy(); }
protected override void Invoke(object parameter) { if (TargetObject != null && TargetName != null) { Type t1 = TargetObject.GetType(); PropertyInfo pInfo = t1.GetProperty(TargetName); if (pInfo != null && this.Val != null) { pInfo.SetValue(TargetObject, this.Val.ToString(), null); } } }
public object GetObject() { Type[] argTypes = ResolveMethodArgTypes(); object[] argValues = PrepareMethodArgs(TargetMethodArgs, argTypes); MethodInfo mInfo = TargetObject.GetType().GetMethod(TargetMethod, argTypes); if (mInfo == null) { throw new MissingMethodException(TargetObject.GetType().ToString(), TargetMethod); } return(mInfo.Invoke(TargetObject, argValues)); }
private void pcn_ValueChanged(object sender, EventArgs e) { if (TargetObject == null) { return; } PropertyInfo p = TargetObject.GetType().GetProperty(TargetProperty, BindingFlags.Public | BindingFlags.Instance); if (p != null) { p.SetValue(TargetObject, this.BindableValue, null); } }
private void HookEvent() { var eventInfo = TargetObject.GetType().GetEvent( _bindingInfo.EventName, BindingFlags.Public | BindingFlags.Instance); if (eventInfo == null) { throw new ArgumentException("eventName"); } eventInfo.RemoveEventHandler(TargetObject, GetEventMethod(eventInfo)); eventInfo.AddEventHandler(TargetObject, GetEventMethod(eventInfo)); }
public override void Initialize() { //Get the event info to access event in the target object EventInfo eventInfo = TargetObject.GetType().GetEvent(Expression.TargetField, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (eventInfo == null) { throw new Exception("EventBinding : event with name " + Expression.TargetField + " does not exists in element of type " + TargetObject.GetType()); } _eventProxy = new EventToCommandProxy(TargetObject, eventInfo, Expression.CommandParameter); _eventProxy.Attach(); }
private void OnEvent(object sender, EventArgs e) { if ((TargetObject == null) || string.IsNullOrEmpty(MethodName)) { return; } var methodInfo = TargetObject.GetType().GetRuntimeMethods().FirstOrDefault(_ => _.Name == MethodName && _.GetParameters().Length == 1 && ((MethodParameter == null) || MethodParameter.GetType().GetTypeInfo().IsAssignableFrom(_.GetParameters()[0].ParameterType.GetTypeInfo()))); methodInfo?.Invoke(TargetObject, new[] { MethodParameter }); }
/// <summary> /// Invoked by an <see cref="Oragon.Spring.Objects.Factory.IObjectFactory"/> /// after it has set all object properties supplied /// (and satisfied <see cref="Oragon.Spring.Objects.Factory.IObjectFactoryAware"/> /// and ApplicationContextAware). /// </summary> /// <remarks> /// <p> /// This method allows the object instance to perform initialization only /// possible when all object properties have been set and to throw an /// exception in the event of misconfiguration. /// </p> /// </remarks> /// <exception cref="System.Exception"> /// In the event of misconfiguration (such as failure to set an essential /// property) or if initialization fails. /// </exception> public void AfterPropertiesSet() { if (TargetType != null && TargetObject != null) { throw new ArgumentException( "Only one of the TargetType or TargetObject properties can be set, not both."); } if (TargetType == null && TargetObject == null) { if (TargetField != null) { throw new ArgumentException( "Specify the TargetType or TargetObject property in combination with the TargetField property."); } // if no other property specified, use the object name for the static field expression... if (StringUtils.IsNullOrEmpty(this.staticField)) { this.staticField = this.objectName; } ParseAndSetFromStaticFieldValue(); } if (TargetField == null) { throw new ArgumentException("The TargetField property is required."); } BindingFlags fieldFlags = BindingFlags.Public | BindingFlags.IgnoreCase; if (TargetObject == null) { // a static field... fieldFlags |= BindingFlags.Static; } else { // an instance field... fieldFlags |= BindingFlags.Instance; TargetType = TargetObject.GetType(); } this.field = targetType.GetField(TargetField, fieldFlags); if (this.field == null) { throw new ObjectDefinitionStoreException( string.Format( CultureInfo.InvariantCulture, "No such field '{0}' on [{1}].", TargetField, TargetObject == null ? TargetType : TargetObject)); } }
public override bool IsValid(out string errorMessage) { MethodInfo methodInfo = Command as MethodBase as MethodInfo; if (methodInfo == null) { errorMessage = "ERROR: Unable to parse command (MemberInfo can't convert to MethodInfo)"; return(false); } if (!methodInfo.IsStatic && TargetObject == null) { errorMessage = "ERROR: Object reference required for non-static method! Please prefix the command with an object reference."; return(false); } if (!methodInfo.IsStatic && TargetObject != null) { MethodInfo targetMethod = TargetObject.GetType().GetMethod(Command.Name); if (targetMethod == null) { errorMessage = "ERROR: Method doesn't exist on object '" + TargetObject + "'!"; return(false); } if (!targetMethod.GetCustomAttributes(true).Any((a) => (a as ConsoleCommand) != null)) { errorMessage = "ERROR: Method does not have the '" + typeof(ConsoleCommand).Name + "' attribute!"; return(false); } } if (Parameters.Count < requiredParameters) { errorMessage = "ERROR: Expected " + requiredParameters + " parameters, but only got " + Parameters.Count + "!"; return(false); } if (Parameters.Count > parameterInfos.Length) { int paramDiff = parameterInfos.Length - requiredParameters; errorMessage = "ERROR: Too many parameters received! Expected " + parameterInfos.Length + (paramDiff != 0 ? "(" + (parameterInfos.Length - requiredParameters) + " optional)" : "") + " but got " + Parameters.Count; return(false); } errorMessage = ""; return(true); }
protected IEnumerable <MethodInfo> GetAllMethods() { if (TargetObject is Type) { #if NET40 return(((Type)TargetObject).GetMethods(BindingFlags.Static | BindingFlags.Public)); #else return(((Type)TargetObject).GetRuntimeMethods()); #endif } #if NET40 return(TargetObject.GetType().GetMethods()); #else return(TargetObject.GetType().GetRuntimeMethods()); #endif }
/// <summary> /// Displays the user interface for the visualizer. /// </summary> /// <param name="windowService"> /// An object of type Microsoft.VisualStudio.DebuggerVisualizers.IDialogVisualizerService, /// which provides methods that a visualizer can use to display Windows forms, controls, and dialogs. /// </param> /// <param name="objectProvider"> /// An object of type Microsoft.VisualStudio.DebuggerVisualizers.IVisualizerObjectProvider. /// This object provides communication from the debugger side of the visualizer /// to the object source (Microsoft.VisualStudio.DebuggerVisualizers.VisualizerObjectSource) /// on the debuggee side. /// </param> protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) { // Get the object to display a visualizer for. try { //TargetObject = (object)objectProvider.GetObject(); //_targetObjectType = TargetObject.GetType(); using (var streamReader = new StreamReader(objectProvider.GetData())) { var targetObjectType = streamReader.ReadLine(); _targetObjectType = Type.GetType(targetObjectType); TargetObject = Serializer.Deserialize(_targetObjectType, streamReader.ReadToEnd()); } } catch (Exception exception) { MessageBox.Show(string.Format(Resources.DeserializationOfXmlFailed, exception.Message)); return; } //Display the view of the object. using (var displayForm = new frmVisualizerDialog(this)) { windowService.ShowDialog(displayForm); if (IsUpdateRequired) { if (objectProvider.IsObjectReplaceable) { //objectProvider.ReplaceObject(TargetObject); //If the object is replaceable and it has been updated then //replace the target object with the updated version. using (var outgoingData = new MemoryStream()) { using (var writer = new StreamWriter(outgoingData)) { writer.WriteLine(TargetObject.GetType().AssemblyQualifiedName); writer.WriteLine(Serializer.Serialize(TargetObject)); writer.Flush(); objectProvider.ReplaceData(outgoingData); } } } } } }
protected MethodInfo FindMethod(Type[] argTypes) { if (TargetObject is Type) { // static method #if NET40 return(((Type)TargetObject).GetMethod(MethodName, BindingFlags.Static | BindingFlags.Public)); #else return(((Type)TargetObject).GetRuntimeMethod(MethodName, argTypes)); #endif } #if NET40 return(TargetObject.GetType().GetMethod(MethodName, argTypes)); #else return(TargetObject.GetType().GetRuntimeMethod(MethodName, argTypes)); #endif }
internal MvvmFxTester(INumberName subjectUnderTest) { _subjectUnderTest = subjectUnderTest; var guineaPig = new TargetObject(); // bind using (var bindingManager = new BindingManager()) { var numberBinding = new TypedBinding <INumberName, INumberName> (guineaPig, t => t.Number, _subjectUnderTest, s => s.Number); var nameBinding = new TypedBinding <INumberName, INumberName> (guineaPig, t => t.FullName, _subjectUnderTest, s => s.FullName); bindingManager.Bindings.Add(numberBinding); bindingManager.Bindings.Add(nameBinding); var testDuration = new Stopwatch(); testDuration.Start(); Run(); testDuration.Stop(); Console.WriteLine( string.Format("Bind to source {0}: {1} msec.", subjectUnderTest.GetType().Name, testDuration.ElapsedMilliseconds.ToString("#,###"))); // unbind bindingManager.Bindings.Remove(numberBinding); bindingManager.Bindings.Remove(nameBinding); //bind back numberBinding = new TypedBinding <INumberName, INumberName> (_subjectUnderTest, t => t.Number, guineaPig, s => s.Number); nameBinding = new TypedBinding <INumberName, INumberName> (_subjectUnderTest, t => t.FullName, guineaPig, s => s.FullName); bindingManager.Bindings.Add(numberBinding); bindingManager.Bindings.Add(nameBinding); testDuration.Restart(); Run(); testDuration.Stop(); Console.WriteLine( string.Format("Bind to target {0}: {1} msec.", guineaPig.GetType().Name, testDuration.ElapsedMilliseconds.ToString("#,###"))); Console.WriteLine(); } }