Code Monkey home page Code Monkey logo

Comments (3)

flts avatar flts commented on August 15, 2024

I did the following at the end of the ObjectWrapper.cs GetOwnProperty method. As you can see I created a new class called DictionaryDescriptor because I couldn't get the ClrAccessDescriptor class working. I will try and create a PullRequest very soon.

// if no methods are found check if target is an IDictionary
if (typeof(IDictionary).IsAssignableFrom(type))
{
    return new DictionaryDescriptor(Engine, propertyName, Target);
}
using System.Globalization;
using System.Reflection;
using Jint.Native;

namespace Jint.Runtime.Descriptors.Specialized
{
    public sealed class DictionaryDescriptor : PropertyDescriptor
    {
        private readonly Engine _engine;
        private readonly object _key;
        private readonly object _item;
        private readonly MethodInfo _getter;
        private readonly MethodInfo _setter;

        public DictionaryDescriptor(Engine engine, string key, object item)
        {
            _engine = engine;
            _item = item;

            _getter = item.GetType().GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public);
            _setter = item.GetType().GetMethod("set_Item", BindingFlags.Instance | BindingFlags.Public);

            _key = _engine.Options.GetTypeConverter().Convert(key, _getter.GetParameters()[0].ParameterType, CultureInfo.InvariantCulture);

            Writable = true;
        }

        public override JsValue? Value
        {
            get
            {
                object[] parameters = { _key };
                return JsValue.FromObject(_engine, _getter.Invoke(_item, parameters));
            }

            set
            {
                var defaultValue = _item.GetType().IsValueType ? System.Activator.CreateInstance(_item.GetType()) : null;
                object[] parameters = { _key, value.HasValue ? value.Value.ToObject() : null };
                _setter.Invoke(_item, parameters);
            }
        }
    }
}

from jint.

sebastienros avatar sebastienros commented on August 15, 2024

Please create a PR a rename it to IndexDescriptor. Even better if you can add some unit tests.

from jint.

flts avatar flts commented on August 15, 2024

Just added PR #56

from jint.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.