Created
July 29, 2017 08:06
-
-
Save runceel/7a6472406d8275cf1827b6af87298c56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mock ApplicationId | |
mock SecretValue1 | |
mock SecretValue2 | |
SetValueAsync(ApplicationId, mock ApplicationId) called. | |
SetValueAsync(SecretValue1, mock SecretValue1) called. | |
SetValueAsync(SecretValue2, mock SecretValue2) called. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApp11 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var h = new KeyVaultHelper(); | |
var s = h.GetValueAsync<Secrets>().Result; | |
Console.WriteLine(s.ApplicationId); | |
Console.WriteLine(s.SecretValue1); | |
Console.WriteLine(s.SecretValue2); | |
h.SetValueAsync(s).Wait(); | |
} | |
} | |
class KeyVaultHelper | |
{ | |
public Task<string> GetValueAsync(string key) | |
{ | |
return Task.FromResult($"mock {key}"); | |
} | |
public Task SetValueAsync(string key, string value) | |
{ | |
Console.WriteLine($"SetValueAsync({key}, {value}) called."); | |
return Task.CompletedTask; | |
} | |
public async Task<T> GetValueAsync<T>() | |
where T: new() | |
{ | |
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public) | |
.Where(x => x.CanRead && x.CanWrite) | |
.Where(x => x.PropertyType == typeof(string)); | |
var result = new T(); | |
foreach (var p in properties) | |
{ | |
p.SetValue(result, await this.GetValueAsync(p.Name)); | |
} | |
return result; | |
} | |
public async Task SetValueAsync<T>(T value) | |
{ | |
var properties = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public) | |
.Where(x => x.CanRead && x.CanWrite) | |
.Where(x => x.PropertyType == typeof(string)); | |
foreach (var p in properties) | |
{ | |
await this.SetValueAsync(p.Name, (string)p.GetValue(value)); | |
} | |
} | |
} | |
class Secrets | |
{ | |
public string ApplicationId { get; set; } | |
public string SecretValue1 { get; set; } | |
public string SecretValue2 { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment