Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@runceel
Created July 29, 2017 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save runceel/7a6472406d8275cf1827b6af87298c56 to your computer and use it in GitHub Desktop.
Save runceel/7a6472406d8275cf1827b6af87298c56 to your computer and use it in GitHub Desktop.
mock ApplicationId
mock SecretValue1
mock SecretValue2
SetValueAsync(ApplicationId, mock ApplicationId) called.
SetValueAsync(SecretValue1, mock SecretValue1) called.
SetValueAsync(SecretValue2, mock SecretValue2) called.
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