2989

Is it possible to add a property to a type, via a DynamicProxy?

Question:

I'm using Castle DynamicProxy to create a proxy of a given type at runtime - including a couple mixins.

I'm trying to figure out if it's possible to also add arbitrary properties to the proxy, e.g.:

class BaseType { string Foo { get; set; } }

and at runtime, I create a new type, that would look like this:

class BaseTypeProxy3848484etc { string Foo { get; set; } OtherType Bar { get; set; } }

In theory, it seems like this <em>should</em> be possible-- maybe I'm just not seeing how to do it with Castle... Any thoughts? Thanks!

Answer1:

Extending DynamicObject you can create a proxy of your instance and add behaviour to the properties you want to add.

Recommend

  • Castle Windsor - Releasing Interceptor with Transient Lifestyle
  • Can I implement an interface in a C# in an .h file?
  • How do you use pagination in a Django REST framework ViewSet subclass?
  • Ember.js ArrayController error
  • Webpack import multiple less files using glob expressions?
  • How to return a deferred promise and create a model with Ember.Deferred?
  • Problem glTexGen in Open GL ES 2.0
  • IIS 7.5 404 Error for .PDF files
  • Use of qualified name in function parameter
  • how to load css classes from my own project specfic css in Sitecore's RAD editor?
  • VB.NET: How do I use coalesce with db column values and nullable types? Or is there a better solutio
  • PHP: Very simple Encode/Decode string
  • Jooq casting String to BigDecimal
  • Autohotkey script running program with command line arguments
  • Calling C function from lua
  • RavenDB indexing errors
  • Multiple Left Join LINQ-to-entities
  • cell spacing in div table
  • Suqueries in select clause with JPA
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • How to return DataSet (xsd) in WCF
  • Default parameter as generic type
  • Suppressing passwd when calling sqlplus from shell script
  • gspread or such: help me get cell coordinates (not value)
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • jQuery .attr() and value
  • Q promise. Difference between .when and .then
  • Azure Cloud Service Web Role web pages do not load
  • Comma separated Values
  • Revoking OAuth Access Token Results in 404 Not Found
  • Error creating VM instance in Google Compute Engine
  • C# - Getting references of reference
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • UserPrincipal.Current returns apppool on IIS