NiL.JS icon indicating copy to clipboard operation
NiL.JS copied to clipboard

Access dictionary entries from object property

Open cyrildurand opened this issue 3 months ago • 11 comments

I try to access dictionary entries from an object property but can't make it works.

Here is a repro code :

        static void Main(string[] args)
        {
            var module = new Module(@"
export default function print(person)
{
   console.log(person.FirstName); 
   console.log(person.Attributes['firstName']); 
   console.log(person.Attributes.firstName); 
}
");

            module.Run();
            var print = (Function)module.Exports.Default;

            var person = new Person()
            {
                FirstName = "test1",
                Attributes = new Dictionary<String, String>() {
                    { "firstName", "test2" }
                }
            };

            print.Call(new Arguments() { person });
        }
    }

I know there is DictionaryWrapper and I can do something like this :

    static void Main(string[] args)
    {
        var module = new Module(@"

export default function print(attributes) { console.log(attributes['firstName']); console.log(attributes.firstName); } ");

        module.Run();
        var print = (Function)module.Exports.Default;

        var person = new Person()
        {
            FirstName = "test1",
            Attributes = new Dictionary<String, String>() {
                { "firstName", "test2" }
            }
        };

        print.Call(new Arguments() { DictionaryWrapper.Of(person.Attributes) });
    }

but I would prefer not changing my method.

I could make my Attributes read/write or virtual. but DictionaryWrapper doesn't implement IDictionary<TKey, TValue> and is sealed.

I also tried by setting MarshalinOptions.DictionaryAsObject or by proxying or wrapping person but could not make it work.

Any ideas ? I could make a pull request to this repo and add IDictionary<TKey, TValue> to DictionaryWrapper if it helps.

cyrildurand avatar Nov 12 '25 20:11 cyrildurand