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 188721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:06:45+00:00 2026-05-11T16:06:45+00:00

I’m trying to get all properties from a type, but using TypeDescriptor.GetProperties(thisType) will only

  • 0

I’m trying to get all properties from a type, but using TypeDescriptor.GetProperties(thisType) will only supply me with properties, that has both setter and a getter. I have write-only properties. Are there a way to retrieve a PropertyDescriptorCollection including those?

/Asger

  • 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-05-11T16:06:45+00:00Added an answer on May 11, 2026 at 4:06 pm

    Write-only properties are a rare beast, and don’t exist in the System.ComponentModel / PropertyDescriptor space. PropertyDescriptors are designed to be readable. I could probably hack HyperDescriptor to shim write-only properties, but it would be a hack – and it would presumably have to throw exceptions for get, which could impact calling code quite a bit.

    As an aside; I generally advise against write-only properties; the text-book example that people trot out is passwords (public string Password {private get;set;}) – I’d much rather have a void SetPassword(string newPassword) method…

    What is it that you actually want to do? There are a range of options here, all very achievable:

    • use reflection alone (slow; maybe not an option)
    • use Delegate.CreateDelegate (very easy)
    • use Expression.Compile (a little harder, but not much)
    • use Reflection.Emit (quite hard)
    • shim write-only properties into PropertyDescriptor (quite hard)

    If you let me know what you actually want to do (rather than the way you are currently trying to do it), I might be able to help more.

    As an example using Delegate.CreateDelegate (note you would want to stash the delegate somewhere and re-use it lots of times):

    edited to show how to do it if you don’t know the specific types at runtime

    using System;
    using System.Reflection;
    
    class Foo
    {
        public string Bar { private get; set; }
        public override string ToString()
        {
            return Bar; // to prove working
        }
    }
    static class Program
    {
        static void Main()
        {
            ISetter setter = Setter.Create(typeof(Foo), "Bar");
            Foo foo = new Foo();
            setter.SetValue(foo, "abc");
            string s = foo.ToString(); // prove working
        }
    }
    public interface ISetter {
        void SetValue(object target, object value);
    }
    public static class Setter
    {
        public static ISetter Create(Type type, string propertyName)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (propertyName == null) throw new ArgumentNullException("propertyName");
            return Create(type.GetProperty(propertyName));
        }
        public static ISetter Create(PropertyInfo property)
        {
            if(property == null) throw new ArgumentNullException("property");
            if (!property.CanWrite) throw new InvalidOperationException("Property cannot be written");
            Type type = typeof(TypedSetter<,>).MakeGenericType(
                    property.ReflectedType, property.PropertyType);
            return (ISetter) Activator.CreateInstance(
                type, property.GetSetMethod());
        }
    }
    
    public class TypedSetter<TTarget, TValue> : ISetter {
        private readonly Action<TTarget, TValue> setter;
        public TypedSetter(MethodInfo method) {
            setter = (Action<TTarget, TValue>)Delegate.CreateDelegate(
                typeof(Action<TTarget, TValue>), method);
        }
        void ISetter.SetValue(object target, object value) {
            setter((TTarget)target, (TValue)value);
        }
        public void SetValue(TTarget target, TValue value) {
            setter(target, value);
        }
    }
    

    Or alternatively using the Expression API (.NET 3.5):

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    
    class Foo
    {
        public string Bar { private get; set; }
        public override string ToString()
        {
            return Bar; // to prove working
        }
    }
    static class Program
    {
        static void Main()
        {
            Action<object,object> setter = Setter.Create(typeof(Foo), "Bar");
            Foo foo = new Foo();
            setter(foo, "abc");
            string s = foo.ToString();
        }
    }
    
    public static class Setter
    {
        public static Action<object,object> Create(Type type, string propertyName)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (propertyName == null) throw new ArgumentNullException("propertyName");
            return Create(type.GetProperty(propertyName));
        }
        public static Action<object,object> Create(PropertyInfo property)
        {
            if(property == null) throw new ArgumentNullException("property");
            if (!property.CanWrite) throw new InvalidOperationException("Property cannot be written");
    
            var objParam = Expression.Parameter(typeof(object), "obj");
            var valueParam = Expression.Parameter(typeof(object), "value");
            var body = Expression.Call(
                Expression.Convert(objParam, property.ReflectedType),
                property.GetSetMethod(),
                Expression.Convert(valueParam, property.PropertyType));
            return Expression.Lambda<Action<object, object>>(
                body, objParam, valueParam).Compile();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 93k
  • Answers 93k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Some subversion repositories will group logically unrelated things (i.e. projects… May 11, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer 1) What font faces are preferred for a website? See… May 11, 2026 at 6:40 pm
  • Editorial Team
    Editorial Team added an answer TextWrangler is a free Mac text editor, which has PHP… May 11, 2026 at 6:40 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.