当前位置: 首页>>代码示例>>C#>>正文


C# Type.GetInterfaceMap方法代码示例

本文整理汇总了C#中System.Type.GetInterfaceMap方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetInterfaceMap方法的具体用法?C# Type.GetInterfaceMap怎么用?C# Type.GetInterfaceMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Type的用法示例。


在下文中一共展示了Type.GetInterfaceMap方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetHandlerAction

        private static Action<object, object> GetHandlerAction(Type typeThatImplementsHandler, Type messageType)
        {
            Type interfaceGenericType = typeof (IHandleMessage<>);
            var interfaceType = interfaceGenericType.MakeGenericType(messageType);

            if (interfaceType.IsAssignableFrom(typeThatImplementsHandler))
            {
                var methodInfo = typeThatImplementsHandler.GetInterfaceMap(interfaceType).TargetMethods.FirstOrDefault();

                if (methodInfo != null)
                {
                    ParameterInfo firstParameter = methodInfo.GetParameters().First();

                    if (firstParameter.ParameterType != messageType)
                        return null;

                    var handler = Expression.Parameter(typeof (object));
                    var message = Expression.Parameter(typeof (object));

                    var castTarget = Expression.Convert(handler, typeThatImplementsHandler);
                    var castParam = Expression.Convert(message, methodInfo.GetParameters().First().ParameterType);
                    var execute = Expression.Call(castTarget, methodInfo, castParam);
                    return Expression.Lambda<Action<object, object>>(execute, handler, message).Compile();
                }
            }

            return null;
        }
开发者ID:kehinze,项目名称:Hermes,代码行数:28,代码来源:ComponentScanner.cs

示例2: GetHandleMethods

        static IDictionary<Type, MethodInfo> GetHandleMethods(Type targetType, Type messageType)
        {
            var result = new Dictionary<Type, MethodInfo>();
            foreach (var handlerInterface in handlerInterfaces)
            {
                MethodInfo method = null;
                try
                {
                    method = targetType.GetInterfaceMap(handlerInterface.MakeGenericType(messageType))
                        .TargetMethods
                        .FirstOrDefault();

                }
                catch
                {
                    //intentionally swallow
                }

                if (method != null)
                   result.Add(handlerInterface,method);

            }

            return result;
        }
开发者ID:JoeyCyril,项目名称:NServiceBus,代码行数:25,代码来源:HandlerInvocationCache.cs

示例3: CreateCloneWrapper

        private static CloneHandler CreateCloneWrapper(Type type)
        {
            var cloneMethod = new DynamicMethod
            (
                "NativeClone",
                MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.Final | MethodAttributes.NewSlot,
                CallingConventions.Standard,
                typeof(IntPtr), new Type[] { type },
                type, false
            );
            var ilGenerator = cloneMethod.GetILGenerator();
            ilGenerator.Emit(OpCodes.Ldarg_0); // Step 1: Push the object to clone on the stack
            // Just to be clean, don't suppose ICloneable only has one member…
            var cloneableInterfaceMap = type.GetInterfaceMap(typeof(ICloneable));
            for (int i = 0; i < cloneableInterfaceMap.InterfaceMethods.Length; i++)
                if (cloneableInterfaceMap.InterfaceMethods[i].Name == "Clone")
                {
                    ilGenerator.Emit(OpCodes.Call, cloneableInterfaceMap.TargetMethods[i]); // Step 2: clone it
                    goto CloneMethodFound; // Finish the job once we found the Clone method (which should always be found)
                }
            throw new InvalidOperationException(); // This line should never be reached
            CloneMethodFound:
            ilGenerator.Emit(OpCodes.Isinst, type); // Step 3: Cast it to the correct type
            var nativePointerProperty = type.GetProperty
            (
                "NativePointer",
                BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder,
                typeof(IntPtr), Type.EmptyTypes, null
            );
            ilGenerator.Emit(OpCodes.Call, nativePointerProperty.GetGetMethod(true)); // Step 4: Get the native pointer
            ilGenerator.Emit(OpCodes.Ret); // Step 5: Return the value

            return cloneMethod.CreateDelegate(typeof(CloneHandler)) as CloneHandler;
        }
开发者ID:nagyist,项目名称:monoxide,代码行数:34,代码来源:CloneFactory.cs

示例4: DefineInterface

        public static void DefineInterface(TypeBuilder typeBuilder, Type interfaceType, Type implementType,
            Action<ILGenerator, MethodInfo, MethodInfo> ilGenerator)
        {
            var proxyMethodBuilder = new ProxyMethodBuilder(typeBuilder);
            InterfaceMapping mapping = implementType.GetInterfaceMap(interfaceType);
            for (int i = 0; i < mapping.InterfaceMethods.Length; i++)
                mapping.TargetMethods[i] = proxyMethodBuilder.DefineMethod(mapping.InterfaceMethods[i],
                    il => ilGenerator(il, mapping.InterfaceMethods[i], mapping.TargetMethods[i]));

            foreach (PropertyInfo propertyInfo in interfaceType.GetProperties())
            {
                MethodBuilder getMethodBuilder = null;
                MethodInfo getMethodInfo = propertyInfo.GetGetMethod();
                if (getMethodInfo != null)
                    getMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, getMethodInfo);

                MethodBuilder setMethodBuilder = null;
                MethodInfo setMethodInfo = propertyInfo.GetSetMethod();
                if (setMethodInfo != null)
                    setMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, setMethodInfo);

                ProxyBuilderHelper.DefineProperty(typeBuilder, propertyInfo, getMethodBuilder, setMethodBuilder);
            }

            foreach (EventInfo eventInfo in interfaceType.GetEvents())
            {
                var addMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, eventInfo.GetAddMethod());
                var removeMethodBuilder = (MethodBuilder)GetTargetMethodInfo(ref mapping, eventInfo.GetRemoveMethod());
                ProxyBuilderHelper.DefineEvent(typeBuilder, eventInfo, addMethodBuilder, removeMethodBuilder);
            }
        }
开发者ID:voronov-maxim,项目名称:ProxyMixin,代码行数:31,代码来源:ProxyBuilderHelper.cs

示例5: FindMethods

        /// <summary>
        ///     Find methods of the supplied type which have WebGet or WebInvoke attributes.
        /// </summary>
        /// <param name="basePath">Base service basePath.</param>
        /// <param name="serviceType">The implementation type to search.</param>
        /// <param name="definitionsTypesList">Types to be documented in the models section.</param>
        internal IEnumerable<Path> FindMethods(string basePath, Type serviceType, IList<Type> definitionsTypesList)
        {
            var paths = new List<Path>();
            var pathActions = new List<Tuple<string, PathAction>>();

            if (!basePath.EndsWith("/"))
                basePath = basePath + "/";

            //search all interfaces for this type for potential DataContracts, and build a set of items
            Type[] interfaces = serviceType.GetInterfaces();
            foreach (Type i in interfaces)
            {
                Attribute dc = i.GetCustomAttribute(typeof(ServiceContractAttribute));
                if (dc == null)
                    continue;

                //found a DataContract, now get a service map and inspect the methods for WebGet/WebInvoke
                InterfaceMapping map = serviceType.GetInterfaceMap(i);
                pathActions.AddRange(GetActions(map, definitionsTypesList));
            }

            foreach (Tuple<string, PathAction> pathAction in pathActions)
            {
                GetPath(basePath, pathAction.Item1, paths).Actions.Add(pathAction.Item2);
            }

            return paths;
        }
开发者ID:Robin--,项目名称:swaggerwcf,代码行数:34,代码来源:Mapper.cs

示例6: GetHandleMethods

        static IDictionary<Type, MethodInfo> GetHandleMethods(Type targetType, Type messageType)
        {
            var result = new Dictionary<Type, MethodInfo>();

            foreach (var handlerInterface in handlerInterfaces)
            {
                MethodInfo method = null;

                var interfaceType = handlerInterface.MakeGenericType(messageType);

                if (interfaceType.IsAssignableFrom(targetType))
                {
                    method = targetType.GetInterfaceMap(interfaceType)
                        .TargetMethods
                        .FirstOrDefault();
                }

                if (method != null)
                {
                    result.Add(handlerInterface, method);
                }
            }

            return result;
        }
开发者ID:nghead,项目名称:NServiceBus,代码行数:25,代码来源:HandlerInvocationCache.cs

示例7: DoGetInterceptableMethods

        private IEnumerable<MethodImplementationInfo> DoGetInterceptableMethods(Type interceptedType, Type implementationType)
        {
            var interceptableMethodsToInterfaceMap = new Dictionary<MethodInfo, MethodInfo>();

            foreach (MethodInfo method in
                implementationType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (MethodOverride.MethodCanBeIntercepted(method))
                {
                    interceptableMethodsToInterfaceMap[method] = null;
                }
            }

            foreach(Type itf in implementationType.GetInterfaces())
            {
                var mapping = implementationType.GetInterfaceMap(itf);
                for(int i = 0; i < mapping.InterfaceMethods.Length; ++i)
                {
                    if(interceptableMethodsToInterfaceMap.ContainsKey(mapping.TargetMethods[i]))
                    {
                        interceptableMethodsToInterfaceMap[mapping.TargetMethods[i]] = mapping.InterfaceMethods[i];
                    }
                }
            }

            foreach(var kvp in interceptableMethodsToInterfaceMap)
            {
                yield return new MethodImplementationInfo(kvp.Value, kvp.Key);
            }
        }
开发者ID:shhyder,项目名称:MapApplication,代码行数:30,代码来源:VirtualMethodInterceptor.cs

示例8: IsDefinedOnType

        private bool IsDefinedOnType(MemberInfo member, Type type)
        {
            if (member.DeclaringType != type)
            {
                return false;
            }

            if (member.MemberType == MemberTypes.Property && !type.IsInterface)
            {
                var property = (PropertyInfo) member;

                IEnumerable<MethodInfo> interfaceMethods =
                    (from i in type.GetInterfaces()
                     from method in type.GetInterfaceMap(i).TargetMethods
                     select method);

                bool exists = (from method in interfaceMethods
                               where property.GetAccessors().Contains(method)
                               select 1).Count() > 0;

                if (exists) return false;
            }

            return true;
        }
开发者ID:lamiomni,项目名称:questionyourfriends,代码行数:25,代码来源:DefaultTypeConvention.cs

示例9: CreateServiceInfo

    public static XmlRpcServiceInfo CreateServiceInfo(Type type)
    {
      XmlRpcServiceInfo svcInfo = new XmlRpcServiceInfo();
      // extract service info
      XmlRpcServiceAttribute svcAttr = (XmlRpcServiceAttribute)
        Attribute.GetCustomAttribute(type, typeof(XmlRpcServiceAttribute));
      if (svcAttr != null && svcAttr.Description != "")
        svcInfo.doc = svcAttr.Description;
      if (svcAttr != null && svcAttr.Name != "")
        svcInfo.Name = svcAttr.Name;
      else
        svcInfo.Name = type.Name;
      // extract method info
      var methods = new Dictionary<string, XmlRpcMethodInfo>();

      foreach (Type itf in type.GetInterfaces())
      {
        XmlRpcServiceAttribute itfAttr = (XmlRpcServiceAttribute)
          Attribute.GetCustomAttribute(itf, typeof(XmlRpcServiceAttribute));
        if (itfAttr != null)
          svcInfo.doc = itfAttr.Description;
#if (!COMPACT_FRAMEWORK)
        InterfaceMapping imap = type.GetInterfaceMap(itf);
        foreach (MethodInfo mi in imap.InterfaceMethods)
        {
          ExtractMethodInfo(methods, mi, itf);
        }
#else
        foreach (MethodInfo mi in itf.GetMethods())
        {
          ExtractMethodInfo(methods, mi, itf);
        }
#endif
      }

      foreach (MethodInfo mi in type.GetMethods())
      {
        var mthds = new List<MethodInfo>();
        mthds.Add(mi);
        MethodInfo curMi = mi;
        while (true)
        {
          MethodInfo baseMi = curMi.GetBaseDefinition();
          if (baseMi.DeclaringType == curMi.DeclaringType)
            break;
          mthds.Insert(0, baseMi);
          curMi = baseMi;
        }
        foreach (MethodInfo mthd in mthds)
        {
          ExtractMethodInfo(methods, mthd, type);
        }
      }
      svcInfo.methodInfos = new XmlRpcMethodInfo[methods.Count];
      methods.Values.CopyTo(svcInfo.methodInfos, 0);
      Array.Sort(svcInfo.methodInfos);
      return svcInfo;
    }
开发者ID:adzerk,项目名称:OpenX.Net,代码行数:58,代码来源:XmlRpcServiceInfo.cs

示例10: GetMethodAttribute

        private static CommandAttribute GetMethodAttribute(Type implementationType, Type commandType)
        {
            var method = implementationType
                .GetInterfaceMap(commandType)
                .TargetMethods
                .Single();

            return method.GetCustomAttribute<CommandAttribute>();
        }
开发者ID:jensandresen,项目名称:clee,代码行数:9,代码来源:AttributeHelper.cs

示例11: HasInterfaceMap

 public static void HasInterfaceMap(IDictionary<string, string> expectedMap, Type type, Type interfaceType)
 {
     var map = type.GetInterfaceMap(interfaceType);
       foreach (var entry in expectedMap) {
     int index = map.InterfaceMethods.ToList().FindIndex(method => method.Name == entry.Key);
     Assert.AreNotEqual(-1, index);
     var targetMethod = map.TargetMethods[index].Name;
     Assert.AreEqual(entry.Value, targetMethod);
       }
 }
开发者ID:cessationoftime,项目名称:nroles,代码行数:10,代码来源:ReflectionAssert.cs

示例12: GetHandleMethod

        static MethodInfo GetHandleMethod(Type targetType, Type messageType)
        {
            var method = targetType.GetMethod("Handle", new[] { messageType });
            if (method != null) return method;

            var handlerType = typeof(IMessageHandler<>).MakeGenericType(messageType);
            return targetType.GetInterfaceMap(handlerType)
                .TargetMethods
                .FirstOrDefault();
        }
开发者ID:robertwilczynski,项目名称:NServiceBus,代码行数:10,代码来源:HandlerInvocationCache.cs

示例13: GetHandleMethod

        static MethodInfo GetHandleMethod(Type targetType, Type messageType)
        {
            var method = targetType.GetMethods().FirstOrDefault(m => m.GetParameters().Any(p => p.ParameterType == messageType));

            if (method != null) return method;

            var handlerType = typeof(IMessageHandler<>).MakeGenericType(messageType);
            return targetType.GetInterfaceMap(handlerType)
                .TargetMethods
                .FirstOrDefault();
        }
开发者ID:dwarry,项目名称:NServiceBus,代码行数:11,代码来源:HandlerInvocationCache.cs

示例14: GetInterfacesForMethod

 static IEnumerable<Type> GetInterfacesForMethod(Type type, MethodInfo method)
 {
     foreach (var itf in type.GetInterfaces())
     {
         var interfaceMap = type.GetInterfaceMap(itf);
         if (interfaceMap.TargetMethods.Any(m => m == method))
         {
             yield return itf;
         }
     }
 }
开发者ID:Xamarui,项目名称:BTDB,代码行数:11,代码来源:MethodInf.cs

示例15: MapInterfaceToImplementation

        /// <summary>
        /// Maps the provided <paramref name="interfaceType"/> to the provided <paramref name="implementationType"/>.
        /// </summary>
        /// <param name="interfaceType">The interface type.</param>
        /// <param name="implementationType">The implementation type.</param>
        /// <returns>The mapped interface.</returns>
        private static IReadOnlyDictionary<int, MethodInfo> MapInterfaceToImplementation(
            Type interfaceType,
            Type implementationType)
        {
            var interfaceMapping = implementationType.GetInterfaceMap(interfaceType);

            // Map the interface methods to implementation methods.
            var interfaceMethods = GrainInterfaceData.GetMethods(interfaceType);
            return interfaceMethods.ToDictionary(
                GrainInterfaceData.ComputeMethodId,
                interfaceMethod => GetImplementingMethod(interfaceMethod, interfaceMapping));
        }
开发者ID:REALTOBIZ,项目名称:orleans,代码行数:18,代码来源:InvocationMethodInfoMap.cs


注:本文中的System.Type.GetInterfaceMap方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。