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

  • Home
  • SEARCH
  • 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 9043433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:43:52+00:00 2026-06-16T10:43:52+00:00

I have a class which has an ObservableCollection<int> as a property, and I’m trying

  • 0

I have a class which has an

ObservableCollection<int>

as a property, and I’m trying to change a value inside that property of an instance of that class. Here’s the code I have and that’s getting a TargetException:

object[] index = null;
var originalPropertyName = propertyName;
if (propertyName.Contains("[") && propertyName.Contains("]"))
{
    index = new object[1];
    index[0] = Convert.ToInt32(propertyName.Split('[')[1].Split(']')[0]);
    propertyName = propertyName.Split('[')[0];
}
PropertyInfo pi = item.GetType().GetProperty(propertyName);
PropertyInfo opi = item.GetType().GetProperty(originalPropertyName);
Type pType = index != null ? pi.PropertyType.GetGenericArguments()[0] : pi.PropertyType;
if (pi != null)
{
    object convertedValue = Convert.ChangeType(value, pType);
    if (index == null)
    {
        item.GetType().GetProperty(propertyName).SetValue(item, convertedValue, null);
    }
    else
    {
        //PropertyInfo ipi = pi.PropertyType.GetProperties().Single(p => p.GetIndexParameters().Length > 0);
        //var collection = pi.GetValue(item, index);
        //collection.GetType().GetProperty("Value").SetValue(collection, convertedValue, null);
        var _pi = pi.PropertyType.GetProperty("Item");
        _pi.SetValue(pi, convertedValue, index);
    }
}

How propertyName is obtained isn’t shown above, but in the case of an indexed property, it begins its life as “IndexedProperty[10]” for example.

In the comments after that “else” you can see other things I’ve tried, by reading some other stackoverflow posts and on other forums on how to do this, but I’ve failed so far. Any ideas?

Casting the property to ObservableCollection isn’t feasible, because I want this to be dynamic.

The concept of the whole thing is to have a DataGrid that’s data-bound and have paste work correctly by updating the proper properties of each instance, no matter if the properties are indexed or not. Non-indexed properties work fine, but I can’t get the ObservableCollection ones to work.

  • 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-16T10:43:54+00:00Added an answer on June 16, 2026 at 10:43 am

    A class with an ObservableCollection<int> as a property doesn’t actually have an indexed property in the traditional sense of an indexer. It just has a non-indexed property which itself has an indexer. So you need to use GetValue to start with (without specifying an index) and then fetch the indexer on the result.

    Basically, you need to remember that:

    foo.People[10] = new Person();
    

    is equivalent to:

    var people = foo.People; // Getter
    people[10] = new Person(); // Indexed setter
    

    It looks like you were nearly there with this commented out code:

    //var collection = pi.GetValue(item, index);
    //collection.GetType().GetProperty("Value").SetValue(collection, convertedValue, null);
    

    … but you were applying the index at the wrong point. You want (I think – the question isn’t terribly clear):

    var collection = pi.GetValue(item, null);
    collection.GetType()
              .GetProperty("Item") // Item is the normal name for an indexer
              .SetValue(collection, convertedValue, index);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some class which has a property actor_ of type Actor .
I have ViewModel, Which has a ObservableCollection[Employee] EmpCol , now that ViewModel is bind
I have an class which has a enum property and a boolean property, based
I have a class which has a number of public fields that are marked
I have property UserSet which contains from ObservableCollection<GridRow> . GridRow - my class, which
I have a customer class which has a byte array property. The application uses
I have an ObservableCollection<Data> Items Data has a Columns property on it which is
I have a class which has a function to retrieve children elements from a
I have a class which has a private attribute which is a reference to
I have a class which has a private attribute vector rectVec; class A {

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.