C# use reflection to modify a running process - reflection

I want to use reflection to get and set properties etc. I know how to do this for an object I instance myself using Activator.CreateInstance(), but how do I attach to an already running process in memory and do the same thing?

I doubt that this is possible.
But if you have the source of the process you want to control, you could use remoting or wcf.

Related

How to dynamically implement proxy pattern in flex action script?

I need to implement a proxy pattern for flex action script. The source object contains 40-50 methods, and may keep increasing, so I hope I could avoid implementing individual methods in the source object.
I remember in java, I could use reflection to centralized all the proxy calls, is there any similar mechanism in Flex Action Script?
Thanks.
I'm not sure what exactly you want to accomplish but there is a handy AVM bytecode library from AS3Commons that allows to create classes on runtime. There is example how to generate dynamic proxy.

Rewrite the same asp.net application again

I have a asp.net application which is written in the traditional method [SqlConnection. SqlAdapter.. ..] in data access layer, I can see that every time we are making a connection and using adapters. Code is written in a very dirty manner. I have been given the task to re write the application in standard/best practice way.
i have all stored procedure already written, is there any way I can use a tool like which will help me in writing the business layer and data access layer?
I suggest you to go for codesmith. As you have all the Stored procedure already written you can either go for the LINQ to SQL feature.
For the DAL, you could use Entity Framework 4. The business logic could be wrapped up by hand.
Take a look at fluentAdo.net. You can use it with all your old stored procedures.
But if you really want to rewrite your app, I suggest you to use NHibernate or Entity Framework. They have pretty same set of fetures.

Is it ok to call Powershell from an ASP.Net process

Is there any problems with creating a powershell runspace in an asp.net application and running commands?
I have a basic example working fine but I'm wondering if there are any pitfalls waiting for me.
I'm particularly wondering:
Is this an a light-weight inprocess operation or is it firing up another processes that could lead to unpredictable behaviour.
As long as you're not shelling out to powershell.exe explicitly, instead using Runspace and Pipeline objects directly, you can be assured it's fairly lightweight. Btw, if you are trying to interact with cmdlets through C#, only cmdlets that derive from PSCmdlet need a pipeline; ones that derive from Cmdlet directly can be called without a pipeline via the Invoke method. This is the most lightweight approach.
This should be OK. I've not done this in ASP.NET but I have hosted in-process with a desktop app. There is no external PowerShell process spun up unless you use background jobs. I think the Quest folks are also doing something like this with their Mobile Shell.
As I can see, others recommend using PowerShell. Well, personally, I would pay more attention. Why?
Each web request should be processed as quickly as possible and without any blocking. If the script contains commands that work with network, then there could be some timeouts (e.g. if the computer is not accessible). The processing thread will be blocked for all the time and can't serve any other web request. Then soon you might get Internal Server Error and similar responses.
Besides that (not proved) I suspect that PowerShell consumes more memory than similar code in C#.
I don't claim don't use Powershell, just pay attention ;)

Options for wiring dependencies with NInject

With NInject (preferably 2.0), what options do we have wrt wiring up our object dependencies in a web application?
Can they be defined in an XML configuration file?
Or does it have to be done via code?
There is an extension for xml based configuration: https://github.com/ninject/ninject.extensions.xml
You can do a lot more powerful binding in code though.
Ninject doesn't have XML configuration, sorry but I can't provide a direct link (cos their site has flash elements), but here is a quotation from ninject.org:
Free yourself from XML
Most other .NET dependency injection
frameworks are designed around the use
of XML to declare type bindings.
Rather than forcing you to write
cumbersome and error-prone text,
Ninject arms you with a fluent
interface, which lets you connect the
pieces of your application using
full-fledged code. This means you can
take advantage of the features of the
IDE and compiler, like code completion
and type-safety.
The problem I see with defining bindings in the code only is that you have to add reference to the dll.
You cannot change the binding without adding reference to new dll (removing reference to old one), change code and recompile.
If we had xml config I wouldn't need reference at all, and wouldn't have to recompile.
Right now I have MVC app that is using DI to pass repositories to Controllers. Nothing else then Ninject code for adding bindings uses the concrete implementations of repositories. And still I need to add reference to dll containing the implementations. For only one line of code!
Or maybe there is a possibility to achieve this using Ninject?
What are you looking to achieve? What sort of stuff are you looking to configure? Dynamically selecting a Strategy ? Passing in Port numbers? You could offer a lot more information as to what you're thinking in order to get a better answer [that you can acccept :P].
You need to split the concerns of:
known object wiring (DI)
configuration - generally you'll want to split those into small focused subsets e.g. Strongly Typed config elements vs having a global pool of settings in a big pile mishmashed together a la appSettings
plugins / unknown object wiring (MEF?)
In the first pool, doing it in Code is just the right way and I cant think of any advantage XML would give, esp. in the context of strong names etc.

Real-World examples of Reflection

What are your best examples of using Reflection in production code?
ASP.NET MVC inferring the action and controller to invoke from URL. Routing in general.
Most recently, I had to use reflection to load an assembly (in order to investigate its public types) from just the path of the assembly.
Before Linq To Sql came out...I had to write my own ORM in .NET 2.0.
I used reflection heavily to reflect back on:
Classes to their CRUD stored procedures
Properties to their column names
Which operations were valid for the class
I also used reflection to handle all variable assignment after the results were retreived (all classes inherited an ActiveRecord class that handled the calls into the DAL).
Rough stuff...but after some performance tuning it wasn't half bad.
Pulling data out of an SQL table where you had an ID, a type, and other data
Then you could load "Chevrolet" and work with all of it's methods
I did have occasion to write a Python O/R mapper on one at one point, but it was a proof of concept and never went into production.
I do quite a lot of work that makes extensive use of the system data dictionary on a DBMS (for example a generic slowly-changing dimension loader). It might be argued that this is not dissimilar to reflective programming in principle.
Finally, Python in all its forms is very easy to do reflection with. In fact, it's so good at this that I've used it to poke about with underlying API's in other languages - and use the reflective capabilities to query the underlying interfaces. I have done this with pretty much every reflective mechanism that exists in the Python world: CPython on Python API's and COM API's using makepy, Jython for java API's and IronPython for .Net API's.
In one of my recent apps, an add-in for Kofax Express, I have an option to OCR a file and output a PDF. Since the OCR tool I'm using has a runtime fee, I made the OCR part a seperate assembly. If the file exists, I show the OCR options and late bind the assembly and invoke the required methods and attach to the events with reflection. A simple plug in architecture without interfaces, and saves customers from having to pay royalty fees if they don't need to OCR; we just don't give them the OCR dll.
WPF Databinding:
1) Binding path "(TextBox.Text)" vs "Text"?
If you bind to a path called Text, WPF uses reflection to resolve the name. If you use the class-qualified name, binding avoids the reflection performance hit. Class-qualified names also allows binding to attached properties!
(via http://dotnet.org.za/rudi/archive/2008/03/25/10-things-i-didn-t-know-about-wpf-data-binding.aspx)
NUnit Unit Testing Framework - Not very typical though
CSLA uses reflection a lot
Pretty much any Windows Forms app that supports plugins
My DAL is all reflection based. It reflects on the POCO properties to build SQL.
Within a factory, we use reflection to either pass back a "Dummy" implementation of an interface or a real (hooked to the DB) implementation of an interface, based on the class specified in a properties file (in Java).

Resources