src/Orleans.Core/CodeGeneration/GrainInterfaceUtils.cs
/// <summary>
/// Gets all grain interface methods for a specified type, which may be a class.
/// </summary>
/// <param name="grainType">The grain type.</param>
/// <param name="bAllMethods">Whether to get all methods or only declared methods.</param>
/// <returns>All grain interface methods for a specified type.</returns>
Gets all grain interface methods for a specified type, which may be a class.
Gets all grain interface methods for a specified type which may be a class .
public static MethodInfo[] GetMethods(Type grainType, bool bAllMethods = true)
        {
            var methodInfos = new List<MethodInfo>();
            GetMethodsImpl(grainType, grainType, methodInfos);
            var flags = BindingFlags.Public | BindingFlags.Instance;
            if (!bAllMethods)
                flags |= BindingFlags.DeclaredOnly;
            MethodInfo[] infos = grainType.GetMethods(flags);
            foreach (var methodInfo in infos)
            {
                if (!methodInfos.Contains(methodInfo, MethodInfoComparer.Default))
                {
                    methodInfos.Add(methodInfo);
                }
            }
            return methodInfos.ToArray();
            static void GetMethodsImpl(Type grainType, Type serviceType, List<MethodInfo> methodInfos)
            {
                foreach (var iType in GetGrainInterfaces(serviceType))
                {
                    if (grainType.IsClass)
                    {
                        var mapping = grainType.GetInterfaceMap(iType);
                        foreach (var methodInfo in iType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
                        {
                            foreach (var info in mapping.TargetMethods)
                            {
                                if (info.DeclaringType == grainType && MethodInfoComparer.Default.Equals(methodInfo, info))
                                {
                                    if (!methodInfos.Contains(methodInfo, MethodInfoComparer.Default))
                                        methodInfos.Add(methodInfo);
                                    break;
                                }
                            }
                        }
                    }
                    else if (grainType.IsInterface)
                    {
                        foreach (var methodInfo in iType.GetMethods(BindingFlags.Instance | BindingFlags.Public))
                        {
                            if (!methodInfos.Contains(methodInfo, MethodInfoComparer.Default))
                                methodInfos.Add(methodInfo);
                        }
                    }
                }
            }
            static List<Type> GetGrainInterfaces(Type type)
            {
                var res = new List<Type>();
                if (IsGrainInterface(type))
                {
                    res.Add(type);
                }
                foreach (var interfaceType in type.GetInterfaces())
                {
                    res.Add(interfaceType);
                }
                return res;
            }
            static bool IsGrainInterface(Type t)
            {
                if (t.IsClass)
                    return false;
                if (t == typeof(IGrainObserver) || t == typeof(IAddressable) || t == typeof(IGrainExtension))
                    return false;
                if (t == typeof(IGrain) || t == typeof(IGrainWithGuidKey) || t == typeof(IGrainWithIntegerKey)
                    || t == typeof(IGrainWithGuidCompoundKey) || t == typeof(IGrainWithIntegerCompoundKey))
                    return false;
                if (t == typeof (ISystemTarget))
                    return false;
                return typeof (IAddressable).IsAssignableFrom(t);
            }
        }
public static MethodInfo [ ] GetMethods ( Type grainType , bool bAllMethods = true ) { var methodInfos = new List < MethodInfo > ( ) ; GetMethodsImpl ( grainType , grainType , methodInfos ) ; var flags = BindingFlags . Public | BindingFlags . Instance ; if ( ! bAllMethods ) flags |= BindingFlags . DeclaredOnly ; MethodInfo [ ] infos = grainType . GetMethods ( flags ) ; foreach ( var methodInfo in infos ) { if ( ! methodInfos . Contains ( methodInfo , MethodInfoComparer . Default ) ) { methodInfos . Add ( methodInfo ) ; } } return methodInfos . ToArray ( ) ; static void GetMethodsImpl ( Type grainType , Type serviceType , List < MethodInfo > methodInfos ) { foreach ( var iType in GetGrainInterfaces ( serviceType ) ) { if ( grainType . IsClass ) { var mapping = grainType . GetInterfaceMap ( iType ) ; foreach ( var methodInfo in iType . GetMethods ( BindingFlags . Instance | BindingFlags . Public ) ) { foreach ( var info in mapping . TargetMethods ) { if ( info . DeclaringType == grainType && MethodInfoComparer . Default . Equals ( methodInfo , info ) ) { if ( ! methodInfos . Contains ( methodInfo , MethodInfoComparer . Default ) ) methodInfos . Add ( methodInfo ) ; break ; } } } } else if ( grainType . IsInterface ) { foreach ( var methodInfo in iType . GetMethods ( BindingFlags . Instance | BindingFlags . Public ) ) { if ( ! methodInfos . Contains ( methodInfo , MethodInfoComparer . Default ) ) methodInfos . Add ( methodInfo ) ; } } } } static List < Type > GetGrainInterfaces ( Type type ) { var res = new List < Type > ( ) ; if ( IsGrainInterface ( type ) ) { res . Add ( type ) ; } foreach ( var interfaceType in type . GetInterfaces ( ) ) { res . Add ( interfaceType ) ; } return res ; } static bool IsGrainInterface ( Type t ) { if ( t . IsClass ) return false ; if ( t == typeof ( IGrainObserver ) || t == typeof ( IAddressable ) || t == typeof ( IGrainExtension ) ) return false ; if ( t == typeof ( IGrain ) || t == typeof ( IGrainWithGuidKey ) || t == typeof ( IGrainWithIntegerKey ) || t == typeof ( IGrainWithGuidCompoundKey ) || t == typeof ( IGrainWithIntegerCompoundKey ) ) return false ; if ( t == typeof ( ISystemTarget ) ) return false ; return typeof ( IAddressable ) . IsAssignableFrom ( t ) ; } }
Gets all grain interface methods for a specified type, which may be a class.
Gets all grain interface methods for a specified type which may be a class .
All grain interface methods for a specified type.
All grain interface methods for a specified type .
grainType
Type
The grain type.
The grain type .
bAllMethods
bool
Whether to get all methods or only declared methods.
Whether to get all methods or only declared methods .