0

I have an issue where I copied a method from solution A to solution B, where it does not work properly anymore. It is weird because I explicitly looked for any dependencies, any frameworks that might be missing but couldn't find anything out.

So I have the following:

var ps = new PropertySet();
ps.SetProperty("id", 4711);
ps.SetProperty("guid", Guid.NewGuid());

var s = JsonConvert.SerializeObject(ps, formatting: Formatting.Indented);
var ps2 = JsonConvert.DeserializeObject<PropertySet>(s);

AssertEqualProperty<int>(ps["id"], ps2["id"]);

// ...............................

private static void AssertEqualProperty<T>(JToken expected, JToken current)
{
   Assert.AreEqual(expected.ToObject<T>(), current.ToObject<T>());
}

On the last line, I get an error on ps["id"] and ps2["id"] stating:

Cannot apply indexing with [] to an expression of type PropertySet Cannot access internal "this" here

The class PropertySet in solution A contains this line: internal JToken this[string key] {get... set...}

Could this be the issue? Is there any workaround possible?

  • You say you copied it, you're probably still referencing the old class which is in a different solution and with an indexer marked `internal`. Additionally make sure you didn't accidentally copy the `namespace` as well, AFAIK `internal` is checked by namespace so they'll have to be under the same root namespace not just the same binary. – Prime Mar 26 '21 at 08:09
  • @AlphaDelta well, that is entirely correct. I actually want to reference that class as there are way too many dependencies and I would need to copy much more classes. I am wondering whether it would be possible to make a workaround, without having to either: -> edit the class from solution A -> create new class without the `internal` –  Mar 26 '21 at 08:15
  • 1
    You could probably use reflection to access the indexer, but if you have to do that with code you're capable of modifying the access modifiers on you're probably doing something wrong. That being said (if you just want to use reflection) this might help https://stackoverflow.com/questions/1347936/identifying-a-custom-indexer-using-reflection-in-c-sharp – Prime Mar 26 '21 at 08:30
  • Thank you very much, @AlphaDelta! I will test that out. –  Mar 26 '21 at 09:40

0 Answers0