Exemplo n.º 1
0
        /// <summary>
        /// Returns a string representation of the Flags values selected by the current instance.
        /// </summary>
        public override string ToString()
        {
            FasterflectFlags @this = this;
            List <string>    names = flagNames.Where(kvp => @this.IsSet(kvp.Key))
                                     .Select(kvp => kvp.Value)
                                     .OrderBy(n => n).ToList();
            int           index = 0;
            StringBuilder sb    = new StringBuilder();

            names.ForEach(n => sb.AppendFormat("{0}{1}", n, ++index < names.Count ? " | " : ""));
            return(sb.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> whose members will be set.</param>
        /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the members.</param>
        /// <param name="memberNames">The names of the members to set.</param>
        /// <returns>A <see cref="Fasterflect.MultiSetter"/> which sets the values of the given members.</returns>
        public static MultiSetter MultiSetter(Type type, FasterflectFlags bindingFlags, params string[] memberNames)
        {
            MultiSetCallInfo callInfo = new MultiSetCallInfo(type, bindingFlags, memberNames);
            MultiSetter      value    = MultiSetters.Get(callInfo);

            if (value != null)
            {
                return(value);
            }
            value = (MultiSetter) new MultiSetEmitter(callInfo).GetDelegate();
            MultiSetters.Insert(callInfo, value);
            return(value);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a delegate that can map values from fields and properties on the source object to fields and properties on the target object.
        /// </summary>
        /// <param name="sourceType">The type of the source object.</param>
        /// <param name="targetType">The type of the target object.</param>
        /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the members.</param>
        /// <param name="sourceNames">The member names (Fields, Properties or both) to include on the source.</param>
        /// <param name="targetNames">The member names (Fields, Properties or both) to include on the target.</param>
        /// <returns>An <see cref="ObjectMapper"/> which sets the target using the matching source members.</returns>
        internal static ObjectMapper Mapper(Type sourceType, Type targetType, FasterflectFlags bindingFlags, string[] sourceNames, string[] targetNames)
        {
            MapCallInfo  info  = new MapCallInfo(sourceType, targetType, bindingFlags, sourceNames, targetNames);
            ObjectMapper value = Mappers.Get(info);

            if (value != null)
            {
                return(value);
            }
            value = (ObjectMapper) new MapEmitter(info).GetDelegate();
            Mappers.Insert(info, value);
            return(value);
        }
Exemplo n.º 4
0
        internal static MethodInvoker Method(Type type, string name, FasterflectFlags bindingFlags, MethodInfo method, Type[] genericTypes, Type[] parameterTypes)
        {
            genericTypes = genericTypes ?? Type.EmptyTypes;
            CallInfo      info  = new CallInfo(type, name, bindingFlags, genericTypes, parameterTypes);
            MethodInvoker value = Methods.Get(info);

            if (value != null)
            {
                return(value);
            }
            method = method ?? ReflectLookup.Method(type, genericTypes, name, parameterTypes, bindingFlags) ?? throw new MissingMethodException(type.FullName, name);
            value  = (MethodInvoker) new MethodInvocationEmitter(method).GetDelegate();
            Methods.Insert(info, value);
            return(value);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Returns a new Flags instance returns a new Flags instance with the values from <paramref name="flags"/>
 /// that were not in <paramref name="mask"/> if <paramref name="condition"/> is true, and otherwise returns
 /// the supplied <paramref name="flags"/>.
 /// </summary>
 public static FasterflectFlags ClearIf(FasterflectFlags flags, FasterflectFlags mask, bool condition)
 {
     return(condition ? (FasterflectFlags)(flags & ~mask) : flags);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Returns a new Flags instance with the union of the values from <paramref name="flags"/> and
 /// <paramref name="mask"/> if <paramref name="condition"/> is true, and otherwise returns a new
 /// Flags instance with the values from <paramref name="flags"/> that were not in <paramref name="mask"/>.
 /// </summary>
 public static FasterflectFlags SetOnlyIf(FasterflectFlags flags, FasterflectFlags mask, bool condition)
 {
     return(condition ? flags | mask : (FasterflectFlags)(flags & ~mask));
 }
Exemplo n.º 7
0
 /// <summary>
 /// Returns a new Flags instance with the union of the values from <paramref name="flags"/> and
 /// <paramref name="mask"/> if <paramref name="condition"/> is true, and otherwise returns the
 /// supplied <paramref name="flags"/>.
 /// </summary>
 public static FasterflectFlags SetIf(FasterflectFlags flags, FasterflectFlags mask, bool condition)
 {
     return(condition ? flags | mask : flags);
 }
Exemplo n.º 8
0
 /// <summary>
 /// Returns true if all values in the given <paramref name="mask"/> are not set in the current Flags instance.
 /// </summary>
 public bool IsNotSet(FasterflectFlags mask)
 {
     return((flags & mask) == 0);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Returns true if at least one of the values in the given <paramref name="mask"/> are set in the current Flags instance.
 /// </summary>
 public bool IsAnySet(FasterflectFlags mask)
 {
     return((flags & mask) != 0);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Returns true if all values in the given <paramref name="mask"/> are set in the current Flags instance.
 /// </summary>
 public bool IsSet(FasterflectFlags mask)
 {
     return((flags & mask) == mask);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Creates a delegate that can map values from fields and properties on the source object to fields and properties with the
        /// same name on the target object.
        /// </summary>
        /// <param name="sourceType">The type of the source object.</param>
        /// <param name="targetType">The type of the target object.</param>
        /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the members.</param>
        /// <param name="names">The optional list of member names against which to filter the members that are
        /// to be mapped. If this parameter is an empty string then no name filtering will be applied.</param>
        /// <returns>An <see cref="ObjectMapper"/> which sets the target using the matching source members.</returns>
        /// <returns></returns>
        public static ObjectMapper Mapper(Type sourceType, Type targetType, FasterflectFlags bindingFlags, params string[] names)
        {
            ObjectMapper value = Mapper(sourceType, targetType, bindingFlags, names, names);

            return(value);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Creates a <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.
 /// </summary>
 /// <param name="type">The <see cref="Type"/> that the object that has the <see cref="MethodInfo"/>.</param>
 /// <param name="name">The name of the <see cref="MethodInfo"/>.</param>
 /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="MethodInfo"/>.</param>
 /// <param name="genericTypes">The generic <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
 /// <param name="parameterTypes">The <see cref="Type"/>s of the <see cref="MethodInfo"/>'s parameters.</param>
 /// <returns>A <see cref="MethodInvoker"/> which invokes the given <see cref="MethodInfo"/>.</returns>
 public static MethodInvoker Method(Type type, Type[] genericTypes, string name, FasterflectFlags bindingFlags, params Type[] parameterTypes)
 {
     return(Method(type, name, bindingFlags, null, genericTypes, parameterTypes));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Creates a delegate which can set an indexer matching <paramref name="bindingFlags"/>.
 /// </summary>
 /// <param name="type">The type which the indexer belongs to.</param>
 /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> used to lookup the indexer.</param>
 /// <param name="parameterTypes">The types of the indexer parameters (must be in the right order), plus
 /// the type of the indexer.</param>
 /// <returns>A delegate which can set an indexer.</returns>
 /// <example>
 /// If the indexer is of type <see cref="string"/> and accepts one parameter of type <see langword="int"/>, this
 /// method should be invoked as follow:
 /// <code>
 /// MethodInvoker invoker = type.DelegateForSetIndexer(new Type[]{typeof(int), typeof(string)});
 /// </code>
 /// </example>
 public static MethodInvoker IndexerSetter(Type type, FasterflectFlags bindingFlags, params Type[] parameterTypes)
 {
     return(Method(type, "set_Item", bindingFlags, null, null, parameterTypes));
 }
Exemplo n.º 14
0
 /// <summary>
 /// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.
 /// </summary>
 /// <param name="type">The <see cref="FieldInfo"/> whose value will be set.</param>
 /// <param name="name">The name of the <see cref="FieldInfo"/> to set.</param>
 /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the <see cref="FieldInfo"/>.</param>
 /// <returns>A <see cref="MemberSetter"/> which sets the value of the given <see cref="FieldInfo"/>.</returns>
 public static MemberSetter FieldSetter(Type type, string name, FasterflectFlags bindingFlags)
 {
     return(Setter(type, name, MemberTypes.Field, bindingFlags, null));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Creates a <see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.
 /// </summary>
 /// <param name="type">The <see cref="Type"/> whose <see cref="PropertyInfo"/> will be set.</param>
 /// <param name="name">The name of the <see cref="PropertyInfo"/> to set.</param>
 /// <param name="bindingFlags">The <see cref="BindingFlags"/> or <see cref="FasterflectFlags"/> to filter the member.</param>
 /// <returns>A<see cref="MemberSetter"/> which sets the value of the given <see cref="PropertyInfo"/>.</returns>
 public static MemberSetter PropertySetter(Type type, string name, FasterflectFlags bindingFlags)
 {
     return(Setter(type, name, MemberTypes.Property, bindingFlags, null));
 }
Exemplo n.º 16
0
        internal static MemberSetter Setter(Type type, string name, MemberTypes memberType, FasterflectFlags bindingFlags, MemberInfo memberInfo)
        {
            MemberCallInfo info  = new MemberCallInfo(type, name, memberType, bindingFlags);
            MemberSetter   value = Setters.Get(info);

            if (value != null)
            {
                return(value);
            }
            memberInfo = memberInfo ?? ReflectLookup.Member(info.TargetType, info.MemberName, info.BindingFlags);
            if (memberInfo == null)
            {
                if (memberType == MemberTypes.Field)
                {
                    throw new MissingFieldException(info.TargetType.FullName, info.MemberName);
                }
                throw new MissingMemberException(info.TargetType.FullName, info.MemberName);
            }
            info.MemberType = memberInfo.MemberType;
            value           = (MemberSetter) new MemberSetEmitter(memberInfo).GetDelegate();
            Setters.Insert(info, value);
            return(value);
        }