fody / ionad Goto Github PK
View Code? Open in Web Editor NEWReplaces static method calls.
Replaces static method calls.
I'm evaluating if Fody + Ionad is an option for a project I'm building, and that would require me to write a small console app that I can pass some assembly file paths and then run Fody + Ionad to perform some replacements of static function calls on these assemblies.
Is this a supported scenario for Fody & Ionad? Any pointers on how to go about it?
From ReadMe is not clear, how to specify the scope of replacement. E.g. When calling from test code I want to use DateTimeSubstitute, but when calling from production code I want to use DateTime. Please clarify in documentation.
Btw, why Ionad?
Does Ionad work on static methods. My substitute isn't being called when running my spec.
[StaticReplacement(typeof(AppUtils))]
public static class AppUtilsSubsitute
{
public static void LogException(string func, Exception ex) => Write("Exception", ex.ToJson());
public static void LogError(string err) => Write("Error", err);
public static void LogInfo(string info) => Write("Info", info);
public static void LogWarning(string info) => Write("Warning", info);
public static void LogCypherQuery(string func, ICypherFluentQuery query) => Write(func, query.ToJson());
static void Write(string logType, string message)
{
var dictionary = new Dictionary<string, string> { { logType, message } };
var assembly = Assembly.GetExecutingAssembly();
using (var writer = new StreamWriter(assembly.Location + "Machine.Specifications.log"))
{
writer.Write(dictionary.ToJson());
}
}
public static Dictionary<string, string> Log()
{
var assembly = Assembly.GetExecutingAssembly();
Dictionary<string, string> content;
using (var reader = new StreamReader(assembly.GetManifestResourceStream("Machine.Specifications.log")))
{
content = JsonConvert.DeserializeObject<Dictionary<string, string>>(reader.ReadToEnd());
}
return content;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class EnsurePresencesOfAttribute : ActionFilterAttribute
{
internal virtual string Required { get; }
internal virtual string Param { get; }
public EnsurePresencesOfAttribute(string required = "", string param="request")
{
Required = required;
Param = param;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
Dictionary<string, object> model = actionContext.ActionArguments;
if (model == null || model.Count == 0 || !model.ContainsKey(Param))
{
ValueError(actionContext, $"{Param} parameter");
return;
}
foreach (var requirement in Required.Split(',').Select(e => e.Trim()))
{
if (model[requirement] == null)
{
ValueError(actionContext, requirement);
return;
}
}
base.OnActionExecuting(actionContext);
}
public override Task OnActionExecutingAsync(HttpActionContext context, CancellationToken token)
{
Dictionary<string, object> model = context.ActionArguments;
if(model == null || model.Count == 0 || !model.ContainsKey(Param))
{
ValueError(context, $"{Param} parameter");
return Task.FromResult(0);
}
foreach (var requirement in Required.Split(',').Select(e => e.Trim()))
{
if (model[requirement] == null)
{
ValueError(context, requirement);
Task.FromResult(0);
}
}
return base.OnActionExecutingAsync(context, token);
}
private static void ValueError(HttpActionContext context, string requirement)
{
var action = context.ActionDescriptor.ActionName;
AppUtils.LogError($"{action} Failed : Missing Required Attribute {requirement}. ");
using (var controller = new BaseApiController { Request = new HttpRequestMessage() })
{
controller.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration());
context.Response = controller.InvalidInputResponse();
}
}
}
Ionad always picks up the FIRST method overload with the correct name, and does not implement overloading resolution correctly.
In the below example we get an InvalidProgramException
. But if you swap the order of declaration of the two overloads of TaskRx.Delay, the program runs.
class Program
{
static async Task<int> Main(string[] args)
{
await Task.Delay(100000);
return 0;
}
}
[StaticReplacement(typeof(Task))]
public static class TaskRx
{
public static Task Delay(int delay, CancellationToken cancellationToken)
{
return Observable.Timer(TimeSpan.FromMilliseconds(delay)).ToTask(cancellationToken);
}
public static Task Delay(int delay)
{
return Observable.Timer(TimeSpan.FromMilliseconds(delay)).ToTask();
}
}
My project is using some static class's static method and should not be weaved in production environment. However when I unit test this project, I need to replace the static class's static method to mock it's behavior, How can I achieve this?
The current situation is, when I add the nuget package to my Unit Test project, the to-be-test dll won't be weaved; if I add the nuget package to my to-be-test project, the weaved dll can not be used in production.
What I expect is, when build my unit test project, the to-be-test dll will be copied to my unit test project's output directory and that dll will be weaved.
Any idea?
Fody Version 2 is out and you will need to do an update for your addin to be usable with it
Specifically:
Please ping me if u need help
Fody 4.2.1
Ionad.Fody 1.0.2
MsBuild 15
VS2017
Trying to weave
public static TaskRx
{
public Task Delay(int delay)
{
return Observerable.Timer(delay, RxApp.BackgroundThreadScheduler).ToTask();
}
}
Possibly because I was trying to weave this inside a lambda, will try to create a SSCCE tomorrow at work.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.