Example #1
0
        public static MemberInfo GetMember(CallInfo callInfo)
        {
            var member = callInfo.MemberInfo;
            if (member != null)
                return member;

            if (callInfo.MemberTypes == MemberTypes.Property)
            {
                member = callInfo.TargetType.Property(callInfo.Name, callInfo.BindingFlags);
                if (member == null)
                {
                    const string fmt = "No match for property with name {0} and flags {1} on type {2}.";
                    throw new MissingMemberException( string.Format( fmt, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType ) );
                }
                callInfo.MemberInfo = member;
                return member;
            }
            if (callInfo.MemberTypes == MemberTypes.Field)
            {
                member = callInfo.TargetType.Field(callInfo.Name, callInfo.BindingFlags);
                if (member == null)
                {
                    const string fmt = "No match for field with name {0} and flags {1} on type {2}.";
                    throw new MissingFieldException( string.Format( fmt, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType ) );
                }
                callInfo.MemberInfo = member;
                return member;
            }
            throw new ArgumentException(callInfo.MemberTypes + " is not supported");
        }
Example #2
0
 public static FieldInfo GetField( CallInfo callInfo )
 {
     var field = callInfo.TargetType.Field( callInfo.Name, callInfo.BindingFlags );
     if( field == null )
     {
         const string fmt = "No match for field with name {0} and flags {1} on type {2}.";
         throw new MissingFieldException( string.Format( fmt, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType ) );
     }
     callInfo.MemberInfo = field;
     return field;
 }
Example #3
0
        public static ConstructorInfo GetConstructor(CallInfo callInfo)
        {
            var constructor = callInfo.MemberInfo as ConstructorInfo;
            if (constructor != null)
                return constructor;

            constructor = callInfo.TargetType.Constructor( callInfo.BindingFlags, callInfo.ParamTypes );
            if (constructor == null)
                throw new MissingMemberException("Constructor does not exist");
            callInfo.MemberInfo = constructor;
            callInfo.MethodParamTypes = constructor.GetParameters().ToTypeArray();
            return constructor;
        }
        public static MethodInfo GetMethod(CallInfo callInfo)
        {
            var method = callInfo.MemberInfo as MethodInfo;
            if (method != null)
                return method;
            method = callInfo.TargetType.Method(callInfo.GenericTypes, callInfo.Name, callInfo.ParamTypes, callInfo.BindingFlags);
            if (method == null)
			{
				const string fmt = "No match for method with name {0} and flags {1} on type {2}.";
				throw new MissingMethodException( string.Format( fmt, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType ) );
			}
			callInfo.MemberInfo = method;
        	callInfo.ParamTypes = method.GetParameters().ToTypeArray();
            return method;
        }
 /// <summary>
 /// Create a delegate to invoke a generic method.  See the overload with same parameters except for <paramref name="genericTypes"/>.
 /// </summary>
 /// <seealso cref="DelegateForCallMethod(Type,string,Flags,Type[])"/>
 public static MethodInvoker DelegateForCallMethod(this Type type, Type[] genericTypes, string name, Flags bindingFlags, params Type[] parameterTypes)
 {
     var callInfo = new CallInfo(type, genericTypes, bindingFlags, MemberTypes.Method, name, parameterTypes, null, true);
     return (MethodInvoker)new MethodInvocationEmitter(callInfo).GetDelegate();
 }
Example #6
0
 internal MemberSetEmitter(CallInfo callInfo)
     : base(callInfo)
 {
 }
Example #7
0
 public static MethodInfo GetPropertySetMethod(PropertyInfo propInfo, CallInfo callInfo)
 {
     var method = propInfo.GetSetMethod();
     if( method != null )
         callInfo.MemberInfo = method;
     return method ?? GetPropertyMethod("set_", "setter", callInfo);
 }
Example #8
0
 private static MethodInfo GetPropertyMethod(string infoPrefix, string propertyMethod, CallInfo callInfo)
 {
     var method = callInfo.TargetType.Method(infoPrefix + callInfo.Name, callInfo.BindingFlags);
     if (method == null)
     {
         const string fmt = "No {0} for property {1} with flags {2} on type {3}.";
         throw new MissingFieldException( string.Format( fmt, propertyMethod, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType ) );
     }
     callInfo.MemberInfo = method;
     return method;
 }
 protected BaseEmitter(CallInfo callInfo)
 {
     CallInfo = callInfo;
 }
Example #10
0
 public static PropertyInfo GetProperty( CallInfo callInfo )
 {
     var property = callInfo.TargetType.Property( callInfo.Name, callInfo.BindingFlags );
     if( property == null )
     {
         const string fmt = "No match for property with name {0} and flags {1} on type {2}.";
         throw new MissingMemberException( string.Format( fmt, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType ) );
     }
     callInfo.MemberInfo = property;
     return property;
 }
Example #11
0
 internal MemberSetEmitter(CallInfo callInfo) : base(callInfo)
 {
 }
Example #12
0
 /// <summary>
 /// Creates a delegate which can get the value of the property specified by <param name="name"/>
 /// matching <param name="bindingFlags"/> on the given <param name="type"/>.
 /// </summary>
 public static MemberGetter DelegateForGetPropertyValue( this Type type, string name, Flags bindingFlags )
 {
     var callInfo = new CallInfo(type, null, bindingFlags, MemberTypes.Property, name, null, null, true);
     return (MemberGetter) new MemberGetEmitter( callInfo ).GetDelegate();
 }
Example #13
0
 protected BaseEmitter(CallInfo callInfo)
 {
     CallInfo = callInfo;
 }
Example #14
0
 protected InvocationEmitter(CallInfo callInfo)
     : base(callInfo)
 {
 }
Example #15
0
        private static void RunHashCodeBenchmark()
        {
            var callInfo = new CallInfo(TargetType, null, Flags.InstanceAnyVisibility, MemberTypes.Field, "name",
                                         new[] { typeof(int), typeof(string) }, null,true );
            var callInfoOther = new CallInfo(typeof(CallInfo), null, Flags.InstanceAnyVisibility, MemberTypes.Field, "other",
                                              new[] { typeof(string) }, null, true );
            var sourceInfo = SourceInfo.CreateFromType( new { ID = 42, Name = "Test" }.GetType() );
            var sourceInfoOther = SourceInfo.CreateFromType( new { id = 42, Name = "Test" }.GetType() );

            var initMap = new Dictionary<string, Action> { };
            var actionMap = new Dictionary<string, Action>
                            {
                                { "CallInfo GetHashCode", () => callInfo.GetHashCode() },
                                { "CallInfo Equals Other", () => callInfo.Equals( callInfoOther ) },
                                { "SourceInfo GetHashCode", () => sourceInfo.GetHashCode() },
                                { "SourceInfo Equals Other", () => sourceInfo.Equals( sourceInfoOther ) },
                                { "string GetHashCode", () => "foo".GetHashCode() },
                                { "string Equals", () => "foo".Equals( "bar" ) },
                                { "new CallInfo", () => new CallInfo( TargetType, null,  Flags.InstanceAnyVisibility, MemberTypes.Field, "name",
                                                                      new[] { typeof(int), typeof(string) }, null, true ) },
                                { "new SourceInfo", () => new SourceInfo( TargetType, new[] { "ID", "Name" }, new[] { typeof(int), typeof(string) } ) },
                                { "new SourceInfo anon", () => SourceInfo.CreateFromType( new { ID = 42, Name = "Test" }.GetType() ) },
                            };
            Execute( "HashCode Benchmark", initMap, actionMap );
        }
 public MethodInvocationEmitter( CallInfo callInfo )
     : base(callInfo)
 {
 }
 public MethodInvocationEmitter(CallInfo callInfo) : base(callInfo)
 {
 }
Example #18
0
 /// <summary>
 /// Creates a delegate which can set the value of the field specified by <paramref name="name"/> and
 /// matching <paramref name="bindingFlags"/> on the given <paramref name="type"/>.
 /// </summary>
 public static MemberSetter DelegateForSetFieldValue( this Type type, string name, Flags bindingFlags )
 {
     var callInfo = new CallInfo(type, null, bindingFlags, MemberTypes.Field, name, null, null, false);
     return (MemberSetter) new MemberSetEmitter( callInfo ).GetDelegate();
 }
Example #19
0
        private static MethodInfo GetPropertyMethod(string infoPrefix, string propertyMethod, CallInfo callInfo)
        {
            var method = callInfo.TargetType.Method(infoPrefix + callInfo.Name, callInfo.BindingFlags);

            if (method == null)
            {
                const string fmt = "No {0} for property {1} with flags {2} on type {3}.";
                throw new MissingFieldException(string.Format(fmt, propertyMethod, callInfo.Name, callInfo.BindingFlags, callInfo.TargetType));
            }
            callInfo.MemberInfo = method;
            return(method);
        }