Write Out Class Methods to Log File - c#

I have a need to write out a Class's Methods to a log file. This needs to be done so the end user can verify the calculations within the Methods are correct.
So, 2 questions:
Can this be done?
Do you need to see any of my code to accomplish this? C# or VB will work.

You can use Type.GetMethods() to obtain all the methods of a class (http://msdn.microsoft.com/en-us/library/vstudio/td205ybf).
You will obtain an array of System.Reflection.MethodInfo[] containing all the info you can need about a Method (http://msdn.microsoft.com/en-us/library/vstudio/system.reflection.methodinfo)

This needs to be done so the end user can verify the calculations within the Methods are correct.
This seems like an onerous task to me, is there a reason you want end-users having a view on the internals of your code?
If you want the parameters and the results of the methods being logged I recommend using Postsharp in particular the OnMethodBoundaryAspect. But as I said I'm not sure if this is what you're trying to achieve.

Related

C#: Using WriteError and WriteVerbose in non-cmdlet inheriting class

Apologies for the seemingly odd Title on this. I'm creating some PowerShell modules in C# and I'm getting rather stuck on the above. In BeginProcessing, ProcessRecord etc, I can call WriteWarning, WriterError and WriteVerbose. However, if I want to add Warnings, Errors etc to any of my custom classes, which are instantiated in the above, then I cannot call these because they're not inheriting from cmdlet. I've spent ages searching for an answer on this, but I cannot find the answer. I found an msdn blog post that wrote about this, but they just called these from within ProcessRecord.
I'm guessing I need to send the messages back to ProcessMessage using a pipeline, but I don't understand how I should do this. Can anybody give me any tips on what/how I should be doing ?
You could pass a Func/delegate of the method(s) that you need to your other custom classes. Ideally you want to wrap all of that up in your own ILogger class that you then implement and pass on to your classes. Or you could just pass the class itself to your other custom classes since the methods that you want to call on it are public.

Override a step declaration in SpecFlow?

So at my job we have a core SpecFlow library that our different teams can use for their automation. This library has some declared steps.
For example, the library might have something like this:
When I click the button
However, let's say I want to define my own step declaration that uses that exact same wording. Is it possible to override it?
As #Grasshopper wrote, the step definition are global.
But you could use Scopes to overwrite it.
See http://www.specflow.org/documentation/Scoped-Bindings/
In this case do not forget to specify on every scenario the tag or the original step definition will be called.
It would be a very bad idea to do this, as any scenario that uses this step and fails will be very much harder to understand and debug.
In general using generic library steps in scenarios is also not such a good idea. Scenarios should not contain generic steps or descriptions of HOW things are done. Instead they should contain steps specific to your business context, and these should describe WHAT is being done and WHY its being done.
So instead of
When I click on sign in
And I fill in my email with ...
...
we get the much simpler and more abstract
When I sign in
which is all about WHAT we are doing, and nothing about HOW we are doing it.
You will get a DuplicateStepException if you have a same step (in your case - When I click the button) twice either in the same step definition file or another one. Even if you use a given or then annotation. This is because the step definitions are loaded globally thus resulting in conflict.
Also you cannot extend a stepdefinition or hook containing file as cucumber will throw an error that this is not acceptable. Thus no way you can override behaviour by inheritance.
You will need to write a different step all together, or if possible pass the button as a parameter to the existing step and put in the logic if you are allowed to modify the library code.

How to generate a stacktrace of method that is called

I call a method of another c# assembly, which returns true or false.
Now I want to find out if the method itself calls another method to generate the return value or simply returns true or false because it's hardcoded.
I already solved the problem by looking into the IL code, but but i'm wondering if there is an more generic way to do this by stacktrace?
The stack will show you the calls that lead to the current line, but not a complete history. In other words, you can only see whether a method was called while it is being called. After it returns from a method, the information about what happened inside is lost.
So if you own this method that may or may not be called, or any other methods it would trigger (via event subscription for example), you would be able to place StackFrame.GetFrame in one of them and see where it was coming from. Otherwise, I think the only way to do it would be to duplicate the logic inside the method to work out whether it would have been called.
Use a decompiler like DotPeek or JustDecompile.
http://www.jetbrains.com/decompiler/
http://www.telerik.com/products/decompiler.aspx
Don't waste time on IL (unless you really have lots of time)
Sorry that is not possible because other assembly can be just a .dll which u might have referenced in your project.
If you have ample of time you may try unethical way of breaking the .dll which will let you explore all the source code.

How to programmatically insert code into assembly

I have an .net assembly at C#. I have both: binary and source which has no logger, for example.
All I need is to insert property which will be initialised specific logger. Then I need to introduce logger invoker in all methods. The first way - is manually write property and their invokes. And the second way - is to write another class\method (I suppose in the same assembly) which will do it automatically.
Is it possible? Any suggestions?
I think it is possible, cause it was one of the questions at the interview. But there is no proof that this is possible, and they wanted to hear "no, do this manually".
This is what we call in architectural terms a 'cross cutting concern'. Logging is something that straddles many aspects of an application.
There are features to take care of it in the Microsoft Enterprise Library. The part you want is the Policy Injection library. You can then specify, in the config, methods to match (based on method name/structure) and a function to be called. In this way you can include logging as a proper cross-cutting concern of your app, rather than something which must be manually coded into every method.
It is not possible to alter the execution of a method without altering the source code and recompiling. You could write a wrapper class that would expose all classes and methods which would first call your logger and then the methods, but that's not what they asked.
So the answer to their question is 1. is possible, 2. isn't possible, and if you would have to add logging support, you would need to add it to each method manually.

How to find amount of parameters in a constructor

I'm trying to find a way to determine how many parameters a constructor has.
Now I've built one constructor with no parameters and 1 constructor with 4 parameters.
Is there, in C#, a way to find out how many parameters a used or given constructor has?
Thing is, I'm using a third constructor to read log files. These logs files are read as string[] elements and there should be just as many as there are arguments. If not, I have a corrupt log file.
But I'm using a lot of subclasses and each constructor has more parameters for their specific log-type.
So I wanted to know: is there a method to check the amount of parameters on a constructor?
And yes, this is a school assignment. I don't know what terms to look for really, so the VS2008 object browser is currently not of much use.
You should look at the System.Reflection Namespace. More specifically, you can get a list of the constructors of a class with:
System.Type.GetType("MYClassName").GetConstructors()
It sounds as if you need to re think your code a bit. From your description, having to dynamically determine the number of arguments in a constructor sounds a bit hairy. You might consider a factory design pattern since the type of object created is determined at runtime. If I misunderstand your problem then using reflection as pointed out by other answers will do the trick for you.
i'm not sure exactly what context you need this information, but if you need it dynamically at run-time try the System.Reflection namespace
otherwise the Intellisense drop-list should show you all the constructors available...
The amount of parameters is constant. I've defined them and they're not changing.
What's happening is I'm simulating a sort of publications tree and I'm making divisions in that(a.k.a. subclasses)
Thusly, all the constructors of my subclasses have the parameters or the classes they inherit from.
Thusly, the length is different for each type of publication.
I have a third constructor, just in case I need to visualise my publication data throuhg reading the log file.
But I have to take into account that the log file might be corrupt. Which includes the possibility that there is no data for all my parameters in the log file.
This is why I have to know how to find the amount of parameters in my constructor: I have to check howmuch data there is in my log compared to the amount of parameters I have.
Can't you make a constructor that takes a reference to the log file (or the current raw logfile entry), reads it, and throw an error if there's any problem?
I'm trying to understand why you'd need to look at the number of elements a constructor has. It seems a weak design from what I've seen so far to trust that the number of elements in the log file happens to identify the type of publication to create.
The short answer to your immediate question is what was stated in an earlier answer: reflect on the constructor for the class you're trying to create, and examine its parameters.

Categories