Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:44:09+00:00 2026-06-03T13:44:09+00:00

I need to write a method which takes any object and returns an equivalent

  • 0

I need to write a method which takes any object and returns an equivalent object but by applying html-encode on all public writable string properties/fields if the object is a reference type. If the object is a string it should obviously return html-encode of the string passed. If it’s an enumerable type it should enumerate the type and apply the same logic as above on each of the items in the list.

Does this even sound possible ? I’ve been toying with some code (using ObjectDumper.cs as the starting point) but haven’t been able to get far.

The idea is that i can then apply this as an aspect to all my service methods, such that the returning object fields are safe to be bound with html ui, which is being worked on by several people who sometimes forget triggering the encode on the client side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:44:10+00:00Added an answer on June 3, 2026 at 1:44 pm

    Does this even sound possible ?

    Yes, but the quick and dirty way is to use reflection, which will cause a massive (30-100x) performance hit versus regular property invocation. This may or may not matter to you.

    If you go the reflection route, you could:

    • discover type information by calling GetType() on the instance you want to modify
    • discover all public properties using Type.GetProperties()
    • check PropertyInfo to see that the property is a string or enumerable string
    • obtain the get accessor from the PropertyInfo object: GetGetMethod()
    • get the value
    • convert the value
    • set the value using the set accessor GetSetMethod()

    For IEnumerable, you would need to do a little extra work, i.e get the collection with a late bound call and then enumerate it. Depending on your types, you may also need to ignore certain properties, modify fields, etc.

    If I had to do this (and I question whether or not this is the correct way to address the problem), I would scan the object’s interface with reflection once, and create dynamic getters/setters for each property and cache them in a Dictionary based on type. Subsequent requests to process an object would then use those cached methods and achieve performance similar to normal get/set invocation.

    Enumerations will be a little more trouble to handle but very doable if you are familiar with IL generation and/or expression trees.

    which is being worked on by several people who sometimes forget
    triggering the encode on the client side.

    I think code should be designed to make developer’s lives easier, but how can you account for what people forget? I have nothing against bulletproof code, but who knows what else will be forgotten. Just a thought.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write a tree search method which takes a type parameter T
I'm trying to write a method which takes a Map[K, Collection[V]] and converts it
i need to write a method that takes in a string as a parameter
I'm writing a 'Sudoku' program, and I need to write a method to check
I need to write an extension method on a byte[]. Is that possible?
I need to write a unit test for a method that will print a
I need to write effective and quick method to search byte array for given
I need write an update statement that used multiple tables to determine which rows
I'm trying to write an extension method for System.Web.UI.Control which will search its ControlCollection
In sum: I need to write a List Comprehension in which i refer to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.