Exemplo n.º 1
0
        private void RegisterFunctions()
        {
            foreach (MethodInfo SharedMethod in typeof(T).GetMethods())
            {
                if (SharedMethod.GetCustomAttribute(typeof(NLCCall)) is NLCCall NLCAttribute)
                {
                    if (RemotingMethods.ContainsKey(NLCAttribute.Identifier))
                    {
                        OnServerExceptionOccurred?.Start(this, new OnServerExceptionOccurredEventArgs(new Exception($"Method with identifier {NLCAttribute.Identifier} already exists!")));
                        continue;
                    }

                    var IsAsync        = SharedMethod.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) is AsyncStateMachineAttribute;
                    var ReturnType     = SharedMethod.ReturnType;
                    var HasAsyncResult = ReturnType.IsGenericType && ReturnType.GetGenericTypeDefinition() == typeof(Task <>);

                    if (!HasAsyncResult && IsAsync && ReturnType != typeof(Task))
                    {
                        throw new Exception($"Shared function {NLCAttribute.Identifier} is asynchronous but does not return a Task!");
                    }

                    RemotingMethods.Add(NLCAttribute.Identifier, new RemotingMethod()
                    {
                        Method         = Helpers.CreateMethodWrapper(typeof(T), SharedMethod),
                        WithContext    = NLCAttribute.WithContext,
                        HasAsyncResult = HasAsyncResult
                    });
                }
            }
        }