|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914 |
- // Copyright (c) Microsoft Corporation. All rights reserved.
- // Licensed under the MIT license. See LICENSE file in the project root for full license information.
-
- namespace Microsoft.VisualStudio.TestTools.UnitTesting
- {
- using System;
- using System.Collections.Generic;
- //using System.Diagnostics;
- //using System.Diagnostics.CodeAnalysis;
- using System.Globalization;
- using System.Reflection;
-
- /// <summary>
- /// This class represents the live NON public INTERNAL object in the system
- /// </summary>
- internal class PrivateObject
- {
- #region Data
-
- // bind everything
- private const BindingFlags BindToEveryThing = BindingFlags.Default | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public;
-
- private static BindingFlags constructorFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance | BindingFlags.NonPublic;
-
- private object target; // automatically initialized to null
- private Type originalType; // automatically initialized to null
-
- //private Dictionary<string, LinkedList<MethodInfo>> methodCache; // automatically initialized to null
-
- #endregion
-
- #region Constructors
-
- ///// <summary>
- ///// Initializes a new instance of the <see cref="PrivateObject"/> class that contains
- ///// the already existing object of the private class
- ///// </summary>
- ///// <param name="obj"> object that serves as starting point to reach the private members</param>
- ///// <param name="memberToAccess">the derefrencing string using . that points to the object to be retrived as in m_X.m_Y.m_Z</param>
- //[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj", Justification = "We don't know anything about the object other than that it's an object, so 'obj' seems reasonable")]
- //public PrivateObject(object obj, string memberToAccess)
- //{
- // Helper.CheckParameterNotNull(obj, "obj", string.Empty);
- // ValidateAccessString(memberToAccess);
-
- // PrivateObject temp = obj as PrivateObject;
- // if (temp == null)
- // {
- // temp = new PrivateObject(obj);
- // }
-
- // // Split The access string
- // string[] arr = memberToAccess.Split(new char[] { '.' });
-
- // for (int i = 0; i < arr.Length; i++)
- // {
- // object next = temp.InvokeHelper(arr[i], BindToEveryThing | BindingFlags.Instance | BindingFlags.GetField | BindingFlags.GetProperty, null, CultureInfo.InvariantCulture);
- // temp = new PrivateObject(next);
- // }
-
- // this.target = temp.target;
- // this.originalType = temp.originalType;
- //}
-
- ///// <summary>
- ///// Initializes a new instance of the <see cref="PrivateObject"/> class that wraps the
- ///// specified type.
- ///// </summary>
- ///// <param name="assemblyName">Name of the assembly</param>
- ///// <param name="typeName">fully qualified name</param>
- ///// <param name="args">Argmenets to pass to the constructor</param>
- //public PrivateObject(string assemblyName, string typeName, params object[] args)
- // : this(assemblyName, typeName, null, args)
- //{
- //}
-
- ///// <summary>
- ///// Initializes a new instance of the <see cref="PrivateObject"/> class that wraps the
- ///// specified type.
- ///// </summary>
- ///// <param name="assemblyName">Name of the assembly</param>
- ///// <param name="typeName">fully qualified name</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the constructor to get</param>
- ///// <param name="args">Argmenets to pass to the constructor</param>
- //public PrivateObject(string assemblyName, string typeName, Type[] parameterTypes, object[] args)
- // : this(Type.GetType(string.Format(CultureInfo.InvariantCulture, "{0}, {1}", typeName, assemblyName), false), parameterTypes, args)
- //{
- // Helper.CheckParameterNotNull(assemblyName, "assemblyName", string.Empty);
- // Helper.CheckParameterNotNull(typeName, "typeName", string.Empty);
- //}
-
- ///// <summary>
- ///// Initializes a new instance of the <see cref="PrivateObject"/> class that wraps the
- ///// specified type.
- ///// </summary>
- ///// <param name="type">type of the object to create</param>
- ///// <param name="args">Argmenets to pass to the constructor</param>
- //public PrivateObject(Type type, params object[] args)
- // : this(type, null, args)
- //{
- // Helper.CheckParameterNotNull(type, "type", string.Empty);
- //}
-
- ///// <summary>
- ///// Initializes a new instance of the <see cref="PrivateObject"/> class that wraps the
- ///// specified type.
- ///// </summary>
- ///// <param name="type">type of the object to create</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the constructor to get</param>
- ///// <param name="args">Argmenets to pass to the constructor</param>
- //public PrivateObject(Type type, Type[] parameterTypes, object[] args)
- //{
- // Helper.CheckParameterNotNull(type, "type", string.Empty);
- // object o;
- // if (parameterTypes != null)
- // {
- // ConstructorInfo ci = type.GetConstructor(BindToEveryThing, null, parameterTypes, null);
- // if (ci == null)
- // {
- // throw new ArgumentException(FrameworkMessages.PrivateAccessorConstructorNotFound);
- // }
-
- // try
- // {
- // o = ci.Invoke(args);
- // }
- // catch (TargetInvocationException e)
- // {
- // Debug.Assert(e.InnerException != null, "Inner exception should not be null.");
- // if (e.InnerException != null)
- // {
- // throw e.InnerException;
- // }
-
- // throw;
- // }
- // }
- // else
- // {
- // o = Activator.CreateInstance(type, constructorFlags, null, args, null);
- // }
-
- // this.ConstructFrom(o);
- //}
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PrivateObject"/> class that wraps
- /// the given object.
- /// </summary>
- /// <param name="obj">object to wrap</param>
- //[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj", Justification = "We don't know anything about the object other than that it's an object, so 'obj' seems reasonable")]
- public PrivateObject(object obj)
- {
- Helper.CheckParameterNotNull(obj, "obj", string.Empty);
- this.ConstructFrom(obj);
- }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="PrivateObject"/> class that wraps
- /// the given object.
- /// </summary>
- /// <param name="obj">object to wrap</param>
- /// <param name="type">PrivateType object</param>
- //[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "obj", Justification = "We don't know anything about the object other than that it's an an object, so 'obj' seems reasonable")]
- public PrivateObject(object obj, PrivateType type)
- {
- Helper.CheckParameterNotNull(type, "type", string.Empty);
- this.target = obj;
- this.originalType = type.ReferencedType;
- }
-
- #endregion
-
- ///// <summary>
- ///// Gets or sets the target
- ///// </summary>
- //public object Target
- //{
- // get
- // {
- // return this.target;
- // }
-
- // set
- // {
- // Helper.CheckParameterNotNull(value, "Target", string.Empty);
- // this.target = value;
- // this.originalType = value.GetType();
- // }
- //}
-
- ///// <summary>
- ///// Gets the type of underlying object
- ///// </summary>
- //public Type RealType
- //{
- // get
- // {
- // return this.originalType;
- // }
- //}
-
- //private Dictionary<string, LinkedList<MethodInfo>> GenericMethodCache
- //{
- // get
- // {
- // if (this.methodCache == null)
- // {
- // this.BuildGenericMethodCacheForType(this.originalType);
- // }
-
- // Debug.Assert(this.methodCache != null, "Invalid method cache for type.");
-
- // return this.methodCache;
- // }
- //}
-
- /// <summary>
- /// returns the hash code of the target object
- /// </summary>
- /// <returns>int representing hashcode of the target object</returns>
- public override int GetHashCode()
- {
- //Debug.Assert(this.target != null, "target should not be null.");
- return this.target.GetHashCode();
- }
-
- /// <summary>
- /// Equals
- /// </summary>
- /// <param name="obj">Object with whom to compare</param>
- /// <returns>returns true if the objects are equal.</returns>
- public override bool Equals(object obj)
- {
- if (this != obj)
- {
- //Debug.Assert(this.target != null, "target should not be null.");
- if (typeof(PrivateObject) == obj?.GetType())
- {
- return this.target.Equals(((PrivateObject) obj).target);
- } else
- {
- return false;
- }
- }
-
- return true;
- }
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, params object[] args)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // return this.Invoke(name, null, args, CultureInfo.InvariantCulture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the method to get.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, Type[] parameterTypes, object[] args)
- //{
- // return this.Invoke(name, parameterTypes, args, CultureInfo.InvariantCulture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the method to get.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <param name="typeArguments">An array of types corresponding to the types of the generic arguments.</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, Type[] parameterTypes, object[] args, Type[] typeArguments)
- //{
- // return this.Invoke(name, BindToEveryThing, parameterTypes, args, CultureInfo.InvariantCulture, typeArguments);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <param name="culture">Culture info</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, object[] args, CultureInfo culture)
- //{
- // return this.Invoke(name, null, args, culture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the method to get.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <param name="culture">Culture info</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, Type[] parameterTypes, object[] args, CultureInfo culture)
- //{
- // return this.Invoke(name, BindToEveryThing, parameterTypes, args, culture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, BindingFlags bindingFlags, params object[] args)
- //{
- // return this.Invoke(name, bindingFlags, null, args, CultureInfo.InvariantCulture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the method to get.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, BindingFlags bindingFlags, Type[] parameterTypes, object[] args)
- //{
- // return this.Invoke(name, bindingFlags, parameterTypes, args, CultureInfo.InvariantCulture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <param name="culture">Culture info</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, BindingFlags bindingFlags, object[] args, CultureInfo culture)
- //{
- // return this.Invoke(name, bindingFlags, null, args, culture);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the method to get.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <param name="culture">Culture info</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, BindingFlags bindingFlags, Type[] parameterTypes, object[] args, CultureInfo culture)
- //{
- // return this.Invoke(name, bindingFlags, parameterTypes, args, culture, null);
- //}
-
- ///// <summary>
- ///// Invokes the specified method
- ///// </summary>
- ///// <param name="name">Name of the method</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the method to get.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <param name="culture">Culture info</param>
- ///// <param name="typeArguments">An array of types corresponding to the types of the generic arguments.</param>
- ///// <returns>Result of method call</returns>
- //public object Invoke(string name, BindingFlags bindingFlags, Type[] parameterTypes, object[] args, CultureInfo culture, Type[] typeArguments)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // if (parameterTypes != null)
- // {
- // bindingFlags |= BindToEveryThing | BindingFlags.Instance;
-
- // // Fix up the parameter types
- // MethodInfo member = this.originalType.GetMethod(name, bindingFlags, null, parameterTypes, null);
-
- // // If the method was not found and type arguments were provided for generic paramaters,
- // // attempt to look up a generic method.
- // if ((member == null) && (typeArguments != null))
- // {
- // // This method may contain generic parameters...if so, the previous call to
- // // GetMethod() will fail because it doesn't fully support generic parameters.
-
- // // Look in the method cache to see if there is a generic method
- // // on the incoming type that contains the correct signature.
- // member = this.GetGenericMethodFromCache(name, parameterTypes, typeArguments, bindingFlags, null);
- // }
-
- // if (member == null)
- // {
- // throw new ArgumentException(
- // string.Format(CultureInfo.CurrentCulture, FrameworkMessages.PrivateAccessorMemberNotFound, name));
- // }
-
- // try
- // {
- // if (member.IsGenericMethodDefinition)
- // {
- // MethodInfo constructed = member.MakeGenericMethod(typeArguments);
- // return constructed.Invoke(this.target, bindingFlags, null, args, culture);
- // }
- // else
- // {
- // return member.Invoke(this.target, bindingFlags, null, args, culture);
- // }
- // }
- // catch (TargetInvocationException e)
- // {
- // Debug.Assert(e.InnerException != null, "Inner exception should not be null.");
- // if (e.InnerException != null)
- // {
- // throw e.InnerException;
- // }
-
- // throw;
- // }
- // }
- // else
- // {
- // return this.InvokeHelper(name, bindingFlags | BindingFlags.InvokeMethod, args, culture);
- // }
- //}
-
- ///// <summary>
- ///// Gets the array element using array of subsrcipts for each dimension
- ///// </summary>
- ///// <param name="name">Name of the member</param>
- ///// <param name="indices">the indices of array</param>
- ///// <returns>An arrya of elements.</returns>
- //public object GetArrayElement(string name, params int[] indices)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // return this.GetArrayElement(name, BindToEveryThing, indices);
- //}
-
- ///// <summary>
- ///// Sets the array element using array of subsrcipts for each dimension
- ///// </summary>
- ///// <param name="name">Name of the member</param>
- ///// <param name="value">Value to set</param>
- ///// <param name="indices">the indices of array</param>
- //public void SetArrayElement(string name, object value, params int[] indices)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // this.SetArrayElement(name, BindToEveryThing, value, indices);
- //}
-
- ///// <summary>
- ///// Gets the array element using array of subsrcipts for each dimension
- ///// </summary>
- ///// <param name="name">Name of the member</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="indices">the indices of array</param>
- ///// <returns>An arrya of elements.</returns>
- //public object GetArrayElement(string name, BindingFlags bindingFlags, params int[] indices)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // Array arr = (Array)this.InvokeHelper(name, BindingFlags.GetField | bindingFlags, null, CultureInfo.InvariantCulture);
- // return arr.GetValue(indices);
- //}
-
- ///// <summary>
- ///// Sets the array element using array of subsrcipts for each dimension
- ///// </summary>
- ///// <param name="name">Name of the member</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="value">Value to set</param>
- ///// <param name="indices">the indices of array</param>
- //public void SetArrayElement(string name, BindingFlags bindingFlags, object value, params int[] indices)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // Array arr = (Array)this.InvokeHelper(name, BindingFlags.GetField | bindingFlags, null, CultureInfo.InvariantCulture);
- // arr.SetValue(value, indices);
- //}
-
- ///// <summary>
- ///// Get the field
- ///// </summary>
- ///// <param name="name">Name of the field</param>
- ///// <returns>The field.</returns>
- //public object GetField(string name)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // return this.GetField(name, BindToEveryThing);
- //}
-
- ///// <summary>
- ///// Sets the field
- ///// </summary>
- ///// <param name="name">Name of the field</param>
- ///// <param name="value">value to set</param>
- //public void SetField(string name, object value)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // this.SetField(name, BindToEveryThing, value);
- //}
-
- ///// <summary>
- ///// Gets the field
- ///// </summary>
- ///// <param name="name">Name of the field</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <returns>The field.</returns>
- //public object GetField(string name, BindingFlags bindingFlags)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // return this.InvokeHelper(name, BindingFlags.GetField | bindingFlags, null, CultureInfo.InvariantCulture);
- //}
-
- ///// <summary>
- ///// Sets the field
- ///// </summary>
- ///// <param name="name">Name of the field</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="value">value to set</param>
- //public void SetField(string name, BindingFlags bindingFlags, object value)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // this.InvokeHelper(name, BindingFlags.SetField | bindingFlags, new object[] { value }, CultureInfo.InvariantCulture);
- //}
-
- /// <summary>
- /// Get the field or property
- /// </summary>
- /// <param name="name">Name of the field or property</param>
- /// <returns>The field or property.</returns>
- public object GetFieldOrProperty(string name)
- {
- Helper.CheckParameterNotNull(name, "name", string.Empty);
- return this.GetFieldOrProperty(name, BindToEveryThing);
- }
-
- /// <summary>
- /// Sets the field or property
- /// </summary>
- /// <param name="name">Name of the field or property</param>
- /// <param name="value">value to set</param>
- public void SetFieldOrProperty(string name, object value)
- {
- Helper.CheckParameterNotNull(name, "name", string.Empty);
- this.SetFieldOrProperty(name, BindToEveryThing, value);
- }
-
- /// <summary>
- /// Gets the field or property
- /// </summary>
- /// <param name="name">Name of the field or property</param>
- /// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- /// <returns>The field or property.</returns>
- public object GetFieldOrProperty(string name, BindingFlags bindingFlags)
- {
- Helper.CheckParameterNotNull(name, "name", string.Empty);
- return this.InvokeHelper(name, BindingFlags.GetField | BindingFlags.GetProperty | bindingFlags, null, CultureInfo.InvariantCulture);
- }
-
- /// <summary>
- /// Sets the field or property
- /// </summary>
- /// <param name="name">Name of the field or property</param>
- /// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- /// <param name="value">value to set</param>
- public void SetFieldOrProperty(string name, BindingFlags bindingFlags, object value)
- {
- Helper.CheckParameterNotNull(name, "name", string.Empty);
- this.InvokeHelper(name, BindingFlags.SetField | BindingFlags.SetProperty | bindingFlags, new object[] {value}, CultureInfo.InvariantCulture);
- }
-
- ///// <summary>
- ///// Gets the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>The property.</returns>
- //public object GetProperty(string name, params object[] args)
- //{
- // return this.GetProperty(name, null, args);
- //}
-
- ///// <summary>
- ///// Gets the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the indexed property.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>The property.</returns>
- //public object GetProperty(string name, Type[] parameterTypes, object[] args)
- //{
- // return this.GetProperty(name, BindToEveryThing, parameterTypes, args);
- //}
-
- ///// <summary>
- ///// Set the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="value">value to set</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- //public void SetProperty(string name, object value, params object[] args)
- //{
- // this.SetProperty(name, null, value, args);
- //}
-
- ///// <summary>
- ///// Set the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the indexed property.</param>
- ///// <param name="value">value to set</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- //public void SetProperty(string name, Type[] parameterTypes, object value, object[] args)
- //{
- // this.SetProperty(name, BindToEveryThing, value, parameterTypes, args);
- //}
-
- ///// <summary>
- ///// Gets the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>The property.</returns>
- //public object GetProperty(string name, BindingFlags bindingFlags, params object[] args)
- //{
- // return this.GetProperty(name, bindingFlags, null, args);
- //}
-
- ///// <summary>
- ///// Gets the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the indexed property.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- ///// <returns>The property.</returns>
- //public object GetProperty(string name, BindingFlags bindingFlags, Type[] parameterTypes, object[] args)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
- // if (parameterTypes != null)
- // {
- // PropertyInfo pi = this.originalType.GetProperty(name, bindingFlags, null, null, parameterTypes, null);
- // if (pi == null)
- // {
- // throw new ArgumentException(
- // string.Format(CultureInfo.CurrentCulture, FrameworkMessages.PrivateAccessorMemberNotFound, name));
- // }
-
- // return pi.GetValue(this.target, args);
- // }
- // else
- // {
- // return this.InvokeHelper(name, bindingFlags | BindingFlags.GetProperty, args, null);
- // }
- //}
-
- ///// <summary>
- ///// Sets the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="value">value to set</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- //public void SetProperty(string name, BindingFlags bindingFlags, object value, params object[] args)
- //{
- // this.SetProperty(name, bindingFlags, value, null, args);
- //}
-
- ///// <summary>
- ///// Sets the property
- ///// </summary>
- ///// <param name="name">Name of the property</param>
- ///// <param name="bindingFlags">A bitmask comprised of one or more <see cref="T:System.Reflection.BindingFlags"/> that specify how the search is conducted.</param>
- ///// <param name="value">value to set</param>
- ///// <param name="parameterTypes">An array of <see cref="T:System.Type"/> objects representing the number, order, and type of the parameters for the indexed property.</param>
- ///// <param name="args">Arguments to pass to the member to invoke.</param>
- //public void SetProperty(string name, BindingFlags bindingFlags, object value, Type[] parameterTypes, object[] args)
- //{
- // Helper.CheckParameterNotNull(name, "name", string.Empty);
-
- // if (parameterTypes != null)
- // {
- // PropertyInfo pi = this.originalType.GetProperty(name, bindingFlags, null, null, parameterTypes, null);
- // if (pi == null)
- // {
- // throw new ArgumentException(
- // string.Format(CultureInfo.CurrentCulture, FrameworkMessages.PrivateAccessorMemberNotFound, name));
- // }
-
- // pi.SetValue(this.target, value, args);
- // }
- // else
- // {
- // object[] pass = new object[(args?.Length ?? 0) + 1];
- // pass[0] = value;
- // args?.CopyTo(pass, 1);
- // this.InvokeHelper(name, bindingFlags | BindingFlags.SetProperty, pass, null);
- // }
- //}
-
- #region Private Helpers
-
- ///// <summary>
- ///// Validate access string
- ///// </summary>
- ///// <param name="access"> access string</param>
- //private static void ValidateAccessString(string access)
- //{
- // Helper.CheckParameterNotNull(access, "access", string.Empty);
- // if (access.Length == 0)
- // {
- // throw new ArgumentException(FrameworkMessages.AccessStringInvalidSyntax);
- // }
-
- // string[] arr = access.Split('.');
- // foreach (string str in arr)
- // {
- // if ((str.Length == 0) || (str.IndexOfAny(new char[] { ' ', '\t', '\n' }) != -1))
- // {
- // throw new ArgumentException(FrameworkMessages.AccessStringInvalidSyntax);
- // }
- // }
- //}
-
- /// <summary>
- /// Invokes the memeber
- /// </summary>
- /// <param name="name">Name of the member</param>
- /// <param name="bindingFlags">Additional attributes</param>
- /// <param name="args">Arguments for the invocation</param>
- /// <param name="culture">Culture</param>
- /// <returns>Result of the invocation</returns>
- private object InvokeHelper(string name, BindingFlags bindingFlags, object[] args, CultureInfo culture)
- {
- Helper.CheckParameterNotNull(name, "name", string.Empty);
- //Debug.Assert(this.target != null, "Internal Error: Null reference is returned for internal object");
-
- // Invoke the actual Method
- try
- {
- return this.originalType.InvokeMember(name, bindingFlags, null, this.target, args, culture);
- } catch (TargetInvocationException e)
- {
- //Debug.Assert(e.InnerException != null, "Inner exception should not be null.");
- if (e.InnerException != null)
- {
- throw e.InnerException;
- }
-
- throw;
- }
- }
-
- private void ConstructFrom(object obj)
- {
- Helper.CheckParameterNotNull(obj, "obj", string.Empty);
- this.target = obj;
- this.originalType = obj.GetType();
- }
-
- //private void BuildGenericMethodCacheForType(Type t)
- //{
- // Debug.Assert(t != null, "type should not be null.");
- // this.methodCache = new Dictionary<string, LinkedList<MethodInfo>>();
-
- // MethodInfo[] members = t.GetMethods(BindToEveryThing);
- // LinkedList<MethodInfo> listByName; // automatically initialized to null
-
- // foreach (MethodInfo member in members)
- // {
- // if (member.IsGenericMethod || member.IsGenericMethodDefinition)
- // {
- // if (!this.GenericMethodCache.TryGetValue(member.Name, out listByName))
- // {
- // listByName = new LinkedList<MethodInfo>();
- // this.GenericMethodCache.Add(member.Name, listByName);
- // }
-
- // Debug.Assert(listByName != null, "list should not be null.");
- // listByName.AddLast(member);
- // }
- // }
- //}
-
- ///// <summary>
- ///// Extracts the most appropriate generic method signature from the current private type.
- ///// </summary>
- ///// <param name="methodName">The name of the method in which to search the signature cache.</param>
- ///// <param name="parameterTypes">An array of types corresponding to the types of the parameters in which to search.</param>
- ///// <param name="typeArguments">An array of types corresponding to the types of the generic arguments.</param>
- ///// <param name="bindingFlags"><see cref="BindingFlags"/> to further filter the method signatures.</param>
- ///// <param name="modifiers">Modifiers for parameters.</param>
- ///// <returns>A methodinfo instance.</returns>
- //private MethodInfo GetGenericMethodFromCache(string methodName, Type[] parameterTypes, Type[] typeArguments, BindingFlags bindingFlags, ParameterModifier[] modifiers)
- //{
- // Debug.Assert(!string.IsNullOrEmpty(methodName), "Invalid method name.");
- // Debug.Assert(parameterTypes != null, "Invalid parameter type array.");
- // Debug.Assert(typeArguments != null, "Invalid type arguments array.");
-
- // // Build a preliminary list of method candidates that contain roughly the same signature.
- // var methodCandidates = this.GetMethodCandidates(methodName, parameterTypes, typeArguments, bindingFlags, modifiers);
-
- // // Search of ambiguous methods (methods with the same signature).
- // MethodInfo[] finalCandidates = new MethodInfo[methodCandidates.Count];
- // methodCandidates.CopyTo(finalCandidates, 0);
-
- // if ((parameterTypes != null) && (parameterTypes.Length == 0))
- // {
- // for (int i = 0; i < finalCandidates.Length; i++)
- // {
- // MethodInfo methodInfo = finalCandidates[i];
-
- // if (!RuntimeTypeHelper.CompareMethodSigAndName(methodInfo, finalCandidates[0]))
- // {
- // throw new AmbiguousMatchException();
- // }
- // }
-
- // // All the methods have the exact same name and sig so return the most derived one.
- // return RuntimeTypeHelper.FindMostDerivedNewSlotMeth(finalCandidates, finalCandidates.Length) as MethodInfo;
- // }
-
- // // Now that we have a preliminary list of candidates, select the most appropriate one.
- // return RuntimeTypeHelper.SelectMethod(bindingFlags, finalCandidates, parameterTypes, modifiers) as MethodInfo;
- //}
-
- //private LinkedList<MethodInfo> GetMethodCandidates(string methodName, Type[] parameterTypes, Type[] typeArguments, BindingFlags bindingFlags, ParameterModifier[] modifiers)
- //{
- // Debug.Assert(!string.IsNullOrEmpty(methodName), "methodName should not be null.");
- // Debug.Assert(parameterTypes != null, "parameterTypes should not be null.");
- // Debug.Assert(typeArguments != null, "typeArguments should not be null.");
-
- // LinkedList<MethodInfo> methodCandidates = new LinkedList<MethodInfo>();
- // LinkedList<MethodInfo> methods = null;
-
- // if (!this.GenericMethodCache.TryGetValue(methodName, out methods))
- // {
- // return methodCandidates;
- // }
-
- // Debug.Assert(methods != null, "methods should not be null.");
-
- // foreach (MethodInfo candidate in methods)
- // {
- // bool paramMatch = true;
- // ParameterInfo[] candidateParams = null;
- // Type[] genericArgs = candidate.GetGenericArguments();
- // Type sourceParameterType = null;
-
- // if (genericArgs.Length != typeArguments.Length)
- // {
- // continue;
- // }
-
- // // Since we can't just get the correct MethodInfo from Reflection,
- // // we will just match the number of parameters, their order, and their type
- // var methodCandidate = candidate;
- // candidateParams = methodCandidate.GetParameters();
-
- // if (candidateParams.Length != parameterTypes.Length)
- // {
- // continue;
- // }
-
- // // Exact binding
- // if ((bindingFlags & BindingFlags.ExactBinding) != 0)
- // {
- // int i = 0;
-
- // foreach (ParameterInfo candidateParam in candidateParams)
- // {
- // sourceParameterType = parameterTypes[i++];
-
- // if (candidateParam.ParameterType.ContainsGenericParameters)
- // {
- // // Since we have a generic parameter here, just make sure the IsArray matches.
- // if (candidateParam.ParameterType.IsArray != sourceParameterType.IsArray)
- // {
- // paramMatch = false;
- // break;
- // }
- // }
- // else
- // {
- // if (candidateParam.ParameterType != sourceParameterType)
- // {
- // paramMatch = false;
- // break;
- // }
- // }
- // }
-
- // if (paramMatch)
- // {
- // methodCandidates.AddLast(methodCandidate);
- // continue;
- // }
- // }
- // else
- // {
- // methodCandidates.AddLast(methodCandidate);
- // }
- // }
-
- // return methodCandidates;
- //}
-
- #endregion
- }
- }
|