Dynamically creation some code during runtime [duplicate] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I work in a code base that is quite large and today I found a project that was emitting IL code inside a normal class.
The project containing the IL code being emitted was a implementation of a Service Locator MSDN Desctiption.
What are the advantages of doing this and why would this be done as apposed to using the C# language?

Typically this is done to circumvent the overhead of using reflection, using information only available at runtime.
You would then use reflection, which can be slow depending on what you do, to build a new piece of code that works directly with the data given to it, without using reflection.
Advantages:
Performance
Disadvantages:
Hard to debug
Hard to get right
Hard to read code afterwards
Steep learning curve
So you need to ensure it's really worth the price before embarking on this.
Note that this is a general answer. In the specific case you came across, there is no way to answer why this was done nor which particular advantages (or disadvantages) you would have without actually seeing the code.

There are many uses for this.
One of the more often used scenario is for changing/injecting code on the fly:
.NET CLR Injection: Modify IL Code during Run-time
A good tutorial that help me to understand a good use for it is:
Dynamic... But Fast: The Tale of Three Monkeys, A Wolf and the DynamicMethod and ILGenerator Classes
Good luck

Related

How to organize F# and C# side by side [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
The project I am working on will have to be migrated from C# to F# based on what brings food on the table tomorrow - in other words, the two languages have to live side by side in this project for years to come.
The problem I am faced with repeatedly is how I should organize projects in the solution to meet this need.
I translate pieces from C# to F# all the time, because it frequently shortens the development time and increases the quality. But I end up with C# code that wants to use F# code, and F# code that want to use C# code, in the same layer, or at boundaries between layers or modules. I find myself putting code in the wrong projects just to avoid creating yet another project in some layer.
The situation is not bad now, but I worry that I will paint myself into a corner at some point, unless I can come up with a pattern or a structure, or simply understand the problem better.
Is there some modification to a normal F# architecture - a pattern, a substructure or a superstructure - that is well suited to such a mixed solution?
Are there patterns that are known to be especially helpful in solving this particular challenge?
Use an interface to communicate between the two languages.
Typically when porting, f# is the lower-level dll and C# can call f# as usual.
Now for f# to call c# define an interface in the f# dll (e.g. IClibApi) that is implemented in c# (e.g. ConcreteClibApi extends IClibApi). Create an instance of ConcreteClibApi and pass it to f#. Now f# can call c# thru IClibApi.
Later you can start porting ConcreteClibApi :)

Are unit-tests needed when you have code contracts? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
If you're developing application using the code contracts, you may know, that this concept was introduced in Eiffel programming language.
I have become very confused after trying this concept in my C# application using System.Diagnostics.Contracts.
The main question for me is the next:
Are unit-tests really needed, if you have code contracts?
One of the major differences, that unit-test frameworks usually don't provide, is the possibility to call private methods (except MS fakes library with its shims). It's done, because of supporting composition & the idea, that private methods are covered by public method calls.
As for the code contracts, I can declare Contract.Requires, Contract.Ensures for private methods.
So, why do I need unit-testing, when I have code-contracts, which behavior is very similar.
Thanks
You surely need Unit testing.
With code contracts, you can only have your static contract verification.
There's much more you can do when running your code.
For example, say you are testing a class that depends on IConnectionProvider. What happens when your GetConnection throws? Code contracts won't help you with that.
Ideally you'd be testing the public methods of your class with different inputs, and verifying that it behaves as expected. This will help you find bugs, and in the long run, design better code.
I would say no. By using code contracts you are defining what your code is supposed to do and checking that it is doing it. The unit test does the same thing for the most part so I believe it is redundant to the point that it is not cost effective to write both.

Real time use of abstract, sealed and static class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I've some common functionality that I've to keep in a class, so will I go for static, sealed or abstract class...Does all these types of classes serve the purpose of keeping the common functionality together...where actually the difference lies when I've to go for one
abstract, sealed, static has nothing to do with real time development. It has to do with bring structure within your software, so that the functionality implemented in classes can and should be used in the right way.
After some comments i think this:
I think you can only learn this, by doing it. There isn't a book or epub that will explain you how to do programming. They will show the syntax and some examples. It will be trial and error. Every day you'll face a new challenge.
You'll have to practice it. The best advise is, look what others already created and try to imagine why did they wrote/solve it that way.
I can explain what a static/sealed/abstract class is/does, but it doesn't learn you when to use it.
Back to the question: Define 'real time'.. I think that static/abstract/sealed should NOT be decisive on how you write your 'real-time' software. If you are 'scared' about performance on this level, C# should not be your choise. I would write c++ or if you want a real challenge, try to beat the compilers with asm ;-)
I think you won't measure the 'overhead'
So, use abstract/static/sealed in a right way, so your future collega's/you can read/maintain it.
I use C# for communication (tcp/ip) between a windows computer and a PLC (with delta robots). But it's far from realtime. It's fast enough to keep many robot working with > 100 messages per second.

C# compiler - what optimizations are performed with the 'optimize code' flag turned on? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm planning on gaining some insights into inheritance usage for .NET systems written in C#. I want to analyse Intermediate Language code instead of C# code to make it possible to also look at compiled code. Is there any information available on which optimizations the C# compiler may do when the optimize code flag is enabled?
I'm analysing call behavior related to inheritance graphs (e.g. using polymorphism, reuse methods from base class, etc).
Most questions and resources on the internet say 'minor optimizations' and other vague things. I need to specifically know the changes in semantics that might occur when compiling for release mode. I am not interested in the performance of code.
For example, Scott Hanselman posts in his blog that method inlining will occur in release mode. But that is just one example. This means that What is /optimize C# compiler key intended for? does not answer my question.
http://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx
Eric Lippert (a former principal developer on the C# compiler team) answered this on his blog. A few of the remarks:
Eliminate dead code (branches which are never reached, checks that always return true,...)
nullcheck optimization.
removal of intermittent calls (A(B(C(D))) is rewritten as A(D));
double return calls.
The entire blog has many more examples and I urge you to read it if you want to know about this.

Way to gather online data/information and report it to a program? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I asked about this Q format on meta, and the they said that worded correctly, this should be appropriate. This being said, sorry if I still butchered the wording and just in case I'd like to get this across (as was recommended): I'm not looking for opinions on certain things or how they work, rather just the ways it's possible. I don't mean to sound ignorant and I'm truly sorry if I do, it was just suggested to me to say on Meta.
Now for the Q; What just general language at all would be capable of gathering information from public online websites, then putting it in the program where it could be further processed as just any old variable? I'm new to coding and wanted to do this as a little 'introductory' program, to teach myself some new stuff. Problem being, with my idea, I don't even know where to start. Again, I'm not asking for specific ways to do this, I was just curious what languages are capable of doing this at all? I'd prefer to do it in a Visual Studio's language (no preference of which ones), if that's possible.
In short: Are either Visual C#/C++ capable of gathering information online to be further handled within the program? If not, what languages are?
I agree with the comment that this is a complicated first programming task. However, you'll undoubtedly learn something trying it.
If you already had some experience programming in Python, I'd suggest you took a look at http://scrapy.org/doc/ which is a framework (that is, a bunch of classes and other useful tools) which let you write programs to extract information from web pages. Scrapy does let you concentrate on programming by taking care of some of the nasty details involved in parsing web pages.
Another option is to use a javascript framework, maybe something like node.js.
I've done a fair amount of web scraping, and I usually end up using a combination of utilities which clean up web pages and a variety of XSLT processors. I personally find that combination of technology to be easier to deal with; I don't try to use C-family languages until I've basically wrestled the data into shape. But everyone has their own style.
Good luck!

Categories

Resources