microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Apps/Annotations/ContextAttribute.cs
36lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Reflection; |
| 5 | |
| 6 | using Microsoft.Teams.Api; |
| 7 | using Microsoft.Teams.Api.Activities; |
| 8 | using Microsoft.Teams.Api.Auth; |
| 9 | using Microsoft.Teams.Api.Clients; |
| 10 | using Microsoft.Teams.Apps.Plugins; |
| 11 | using Microsoft.Teams.Common.Extensions; |
| 12 | using Microsoft.Teams.Common.Logging; |
| 13 | using Microsoft.Teams.Common.Storage; |
| 14 | |
| 15 | namespace Microsoft.Teams.Apps.Annotations; |
| 16 | |
| 17 | [AttributeUsage(AttributeTargets.Parameter, Inherited = true)] |
| 18 | public class ContextAttribute : ContextAccessorAttribute |
| 19 | { |
| 20 | public override object? GetValue(IContext<IActivity> context, ParameterInfo parameter) |
| 21 | { |
| 22 | var type = parameter.ParameterType; |
| 23 | |
| 24 | if (type == typeof(ILogger)) return context.Log; |
| 25 | if (type == typeof(IStorage<string, object>)) return context.Storage; |
| 26 | if (type == typeof(IStreamer)) return context.Stream; |
| 27 | if (type.IsAssignableTo(typeof(IActivity))) return context.Activity.ToType(parameter.ParameterType, null); |
| 28 | if (type == typeof(ApiClient)) return context.Api; |
| 29 | if (type == typeof(CancellationToken)) return context.CancellationToken; |
| 30 | if (type == typeof(ConversationReference)) return context.Ref; |
| 31 | if (type == typeof(IContext.Client)) return new IContext.Client(context); |
| 32 | if (type == typeof(IContext.Next)) return new IContext.Next(context.Next); |
| 33 | if (type == typeof(JsonWebToken)) return context.UserGraphToken; |
| 34 | return context; |
| 35 | } |
| 36 | } |