BindServiceMethodAttribute.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Diagnostics.CodeAnalysis;
  18. namespace Grpc.Core
  19. {
  20. /// <summary>
  21. /// Specifies the location of the service bind method for a gRPC service.
  22. /// The bind method is typically generated code and is used to register a service's
  23. /// methods with the server on startup.
  24. ///
  25. /// The bind method signature takes a <see cref="ServiceBinderBase"/> and an optional
  26. /// instance of the service base class, e.g. <c>static void BindService(ServiceBinderBase, GreeterService)</c>.
  27. /// </summary>
  28. [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
  29. public class BindServiceMethodAttribute : Attribute
  30. {
  31. // grpc-dotnet uses reflection to find the bind service method.
  32. // DynamicallyAccessedMembersAttribute instructs the linker to never trim the method.
  33. private const DynamicallyAccessedMemberTypes ServiceBinderAccessibility = DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="BindServiceMethodAttribute"/> class.
  36. /// </summary>
  37. /// <param name="bindType">The type the service bind method is defined on.</param>
  38. /// <param name="bindMethodName">The name of the service bind method.</param>
  39. public BindServiceMethodAttribute([DynamicallyAccessedMembers(ServiceBinderAccessibility)] Type bindType, string bindMethodName)
  40. {
  41. BindType = bindType;
  42. BindMethodName = bindMethodName;
  43. }
  44. /// <summary>
  45. /// Gets the type the service bind method is defined on.
  46. /// </summary>
  47. [DynamicallyAccessedMembers(ServiceBinderAccessibility)]
  48. public Type BindType { get; }
  49. /// <summary>
  50. /// Gets the name of the service bind method.
  51. /// </summary>
  52. public string BindMethodName { get; }
  53. }
  54. }