Generating c# programatically and compiling...issues - c#

Hello all,
We are creating a dll on the fly for security, we are attempting to have a dll generated and pushed to clients as a challenge response system... with this we are generating 20 variables each with a unique guid and then programatically selecting which one is referenced in the code... however once it compiles this it no longer has the extra dummy variables in it, just the one that it used.
Does anyone know what i can change in the compiler options to make it stop doing this or trick it into no optimizing those out?
I've tried
U = "71d41342-e56e-4643-b12f-24df0b4506ae";
System.Diagnostics.Debug.WriteLine(U);
string V = "";
referencing it in debug writeline but that didnt seem to be enough to get it to keep it. i also added /Od to disablke optimizations but this didnt seem to help either! Any help would be appreciated

You have several options to achieve that:
add those variables to a Dictionary in a consturctor and provide a method to iterate that Dictionary
OR
try putting static reaonly string in front of that declarations

Related

pass variables as array specflow c#

I am attempting to use Specflow to automate web tests using Selenium. So far, things are going mostly fine, but I am now running into a problem. One of my steps allow for a user to input a variable, the step looks like this:
Given I click the (VARIABLE) Menu
And the code behind it is fairly simple, just clicking on a link based on the text that is passed:
driver.FindElement(By.XPath("Xpath to get to the variable")).Click();
However, there is a later step that must use this information. That is fine, you can use "ScenarioContext.Current.Add(string, variable)" and I know about that and have been using it. It functions for the needs that I was first informed of.
My problem is that now the business wants to be able to add multiple items at the same time. This presents two problems. Attempting to just call the step a second time throws an exception: "An item with the same key has already been added." and if I put this into a Scenario Outline, which would allow me to call the variable a second time in a second run, I cannot use the first variable in the final step.
Logically, this means that passing in a variable multiple times is the problem (which makes sense, given it's passing in as a string) and so passing the variable in as an array seems the logical way to go. The idea is that when I pass the parameter from one step to another as an array instead of as a string I theoretically won't run into this error and then I will be able to iterate through the items in the array in that later step with a for loop. This seems like something that SpecFlow should be able to do, but I am having issues finding out just how to achieve this. Does anyone have an idea on how to do this? I attempted to merely use:
Scenario.Context.Current.Add(string, variable).ToArray();
However, that does not work, and all of the examples of "ToArray" I can find in the SpecFlow documentation doesn't seem to be actually changing the variables you pass from one step to another into an array, it seems to be used solely inside of individual steps and never passed between steps. Is passing parameters using ScenarioContext.Current.Add(string, variable) as an array possible in SpecFlow?
Thanks in advance.
the simplest solution to your problem is to add an array (or list) to the context in the first step and then to get it out and add to it and then replace it again in future steps:
List<string> list = new List<String>();
list.Add(variable)
ScenarioContext.Current.Add(name, list);
then later
List<String> currentList = (List<String>) ScenarioContext.Current[string];
currentList.Add(variable);
ScenarioContext.Current[name]=list;
However I feel duty bound to point out some issues with your current solution. You should investigate the PageObject pattern and hide your element selection XPath inside your page objects. Imagine the business decides to change the element that information is stored in. Now you have to change every test that does this:
driver.FindElement(By.XPath("Xpath to get to the variable")).Click();
for that variable. Using the page object pattern this is hidden inside the page object and you would only have a single place to change.
I personally would also consider sharing data using context injection as I find this allows strong typing of the data (so no cast is required like in the example above) and it allows you to know what data is stored, its not just a random bag of stuff).

"Source array was not long enough" Error registering BsonClassMap conventions in mongodb with C# driver

In the following code:
var myConventions = new ConventionProfile();
myConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention());
BsonClassMap.RegisterConventions(myConventions, (type) => true);
whenever it calls RegisterConventions, we are getting the following error, but it is very intermittent and random, and so it's very hard to troubleshoot.
"Source array was not long enough. Check srcIndex and length, and the
array's lower bounds."
A few times we've purged our collections and that's fixed it, but the last few times, we purged and still had to then restart the windows service. Then we could use our application and create records and collections to our hearts' content.
I'm thinking perhaps an array in one or more of our mapped class's array properties is getting initialized inconsistently, but I'm drawing a blank on next steps to take. I could perhaps use a field initializer or initialize it in the constructor, but I don't want to start mucking with mongo api's mapping magic. Also, we haven't seen this error in at least a week, so if I made some sort of fix and left it in there, I'll have no way of knowing if it actually fixed the problem or not.
Please help! Thanks!

ASP.NET refering an image or asp file in a Strongly Typed fashion

Many times we need to refer another file on the server. Whether it's just an image, or another asp page. For example:
if (success)
{
img1.ImageUrl = "RightArrow.jpg"
}
The problem here is, if someone accidentally change the string "RightArrow.jpg" to "RghtArrow.jpg", it will not cause a compile time error. And it might take a lot of time to notice the run time error.
So is there any best practice here?
Of course I can build me own little mechanism for that... but I wander if there's anything build-in.
Something which is maybe strongly-typed fashioned:
img1.ImageUrl = Images.RightArrow;
Why not use Settings? If you did your code would be strongly typed, for example:
img1.ImageUrl = Settings.Default.Images.RightArrow
If you have a limited number of these resources this could be a good solution as you could even change the path/name without recompiling...
Can you just keep a List of the names of the pictures?
List<string> CollectionPictureUri = new List<string>();
CollectionPictureUri.Items.Add("RightArrow.jpg");
if (success)
{
img1.ImageUrl = CollectionPictureUri[0];
}
Or if you use a Map, then the key could just be [RightArrow]
Then you can initialize all the items in one block of code and make sure they are correct in one place.
EDIT:
You can also use asserts to validate that your strings resolve to the correct name. http://msdn.microsoft.com/en-us/library/system.diagnostics.debug.assert%28v=vs.110%29.aspx
There are other testing techniques that might be useful to you. With visual studio you can prevent your code from building until errors like this are resolved.
You could always use a T4 template to generate a class with constants for the files which actually exist. This is similar to what the T4 MVC template does for scripts and contents of the /Content directory; it scans those directories when it is run, and creates constants.

Is it possible to return the code of a method (as a string) using delegates in C#?

I'll exemplify, since I'm not sure if I'm asking the question correctly (English is not my primary language, plus I'm still learning C#).
I've started going through Project Euler, and decided to create an application to keep track of my results, and to put my little C# knowledge to test.
In a particular class I hold static functions that are used to solve each of the problems.
static class Answers()
{
public static string A1(){...}
public static string A2(){...}
public static string A3(){...}
//it goes on
}
Problem objects will be created like this (Problem class definition and object creation in runtime).
class Problem
{
public string Description;
public Solution SolutionFinder;
public Problem(string Desc, Solution Solver)
{
this.Description = Desc;
this.SolutionFinder = Solver;
}
public delegate string Solution();
public string SolveProblem()
{
return SolutionFinder.Invoke();
}
}
This is on my Form creation code:
{
...
List<Problem> Euler = new List<Problem>();
Euler.Add(new Problem("Find the sum of all the multiples of 3 or 5 below 1000.", Answers.A1));
Euler.Add(new Problem("By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.", Answers.A2));
Euler.Add(new Problem("What is the largest prime factor of the number 600851475143 ?", Answers.A3));
...
}
I got the classes and even the delegate thing to work correctly, and I'm thrilled with that. Then I finally decided to show the whole thing on a form. What I'm trying to show is: the description of the problem (this is done) and the code for each method (whatever is inside A1, A2, etc.) whenever I solve a problem using my form.
Is that clear? It's just that I want my form to show the result and how I got the solution for each problem, but without having to retype the contents of each method just for display - the methods are already there.
And please don't mind the messy code and overuse of public members: I understand it's a bad practice, but for now I'm just trying to get through this personal project, and I believe it's OK to do this here since it's just a small learning experience.
Thanks.
[EDIT]
The format I'm looking for is:
void UpdateForm(int Current)
{
Problem CurrentProblem = Euler[Current-1];
string Desc = CurrentProblem.Description;
string Code = CurrentProblem.SolutionFinder.Method.ReturnType.Name;
//I got this far, but I need to display more than just the name of the method!
...
}
To Clarify
Given the method:
public static string A1() {
var answer = 1 + 1;
return answer.ToString();
}
Is it possible to obtain the following lines in a string..?
var answer = 1 + 1;
return answer.ToString();
While it's not the fanciest approach, if you set the "Copy to Output Directory" value to "Copy Always" (or "Copy if newer") via the Properties of the source file (right-click/ Properties), you can parse the code file on your own while optimizing for your coding style.
To add, are you aware that you are basically rebuilding NUnit, aside from the source code dump?
Since at runtime the code is in IL (intermediate language), you actually don't have the readable code at your disposal.
Anyway you could use reflection to create an on-fly decompiler, or use third party library like this one or this one to decompile the methods... but I don't know if they expose some API, I just know them as GUI tools.
You might think about storing your solutiuons (source code) in text files.. (eg. Problem1.projecteuler, ProblemN.projecteuler) and loading these text files both for display and for compilation/execution plugin-style. Check out System.Reflection namespace and do some web searches for "C# plugin tutorial" or some such to get started.
valeriano your static methods could be Linq Expressions, then you could iterate through each node of the expression. Using Reflection works too but you need more code
There's no easy way to run code (ie: contents of a method) from a string since C# is already compiled before its executable is launched.
However, there are tricks you can do with Reflection and Diagnostics, but that requires you to run your code in Debug mode. If it's just a small app, this shouldn't be a problem. But larger applications with suffer performance issues when ran in debug mode.
For more info: Execute a string in C# 4.0

code to identify the number

I got one problem while doing one TAPI application based project in C#. I'm using ITAPI3.dll
My problem is.. i'm not getting incoming call information. To get the incoming call information, i'm using the get_callinfo function, but it is showing empty message.
Did you try a different modem?
TAPI is very hardware dependent
This might be a useful MSDN starting point:
http://msdn.microsoft.com/en-us/library/ms726262%28VS.85%29.aspx
(if you didn't already have that url)
I'm just experiencing the same problem. When i debug, a openfiledialog opens asking me to open a file. i'm no sure what it is right now, will get back when i find something. So i just skips the line of code, what causes it to be empty.
I found what was causing the problem for me :
get_callInfo has 3 constructors : one returning object, one returning int and one returning string. For some reason, the one returning object is failing. So i tried the string constructor. This gave me all the information i need. I'll give an overview of all attributes you can choose from :
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLEDIDNUMBER);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLEDIDNAME);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLEDPARTYFRIENDLYNAME);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLERIDNAME);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLERIDNUMBER);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLINGPARTYID);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_COMMENT);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CONNECTEDIDNAME);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_CONNECTEDIDNUMBER);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_DISPLAYABLEADDRESS);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_REDIRECTINGIDNAME);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_REDIRECTINGIDNUMBER);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_REDIRECTIONIDNAME);
e.Call.get_CallInfo(CALLINFO_STRING.CIS_REDIRECTIONIDNUMBER);
hope this still helps

Categories