Ejemplo n.º 1
0
        private IEnumerable <KeyValuePair <string, PropertyDescriptor> > GetObjectProperties(ObjectWrapper objectWrapper)
        {
            var target = objectWrapper.Target;

            if (target is IDictionary dictionary)
            {
                foreach (DictionaryEntry entry in dictionary)
                {
                    var jsValue    = JsValue.FromObject(_scriptEngine, entry.Value);
                    var descriptor = new PropertyDescriptor(jsValue, false, false, false);
                    yield return(new KeyValuePair <string, PropertyDescriptor>(entry.Key.ToString(), descriptor));
                }
                yield break;
            }

            var type = target.GetType();

            if (target is Task task &&
                task.IsCompleted == false)
            {
                foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    if (property.CanRead == false)
                    {
                        continue;
                    }

                    if (property.Name == nameof(Task <int> .Result))
                    {
                        var taskResultDescriptor = JintPreventResolvingTasksReferenceResolver.GetRunningTaskResult(task);
                        yield return(new KeyValuePair <string, PropertyDescriptor>(property.Name, taskResultDescriptor));

                        continue;
                    }

                    var descriptor = new PropertyInfoDescriptor(_scriptEngine, property, target);
                    yield return(new KeyValuePair <string, PropertyDescriptor>(property.Name, descriptor));
                }
                yield break;
            }

            // look for properties
            foreach (var property in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (property.CanRead == false)
                {
                    continue;
                }

                var descriptor = new PropertyInfoDescriptor(_scriptEngine, property, target);
                yield return(new KeyValuePair <string, PropertyDescriptor>(property.Name, descriptor));
            }

            // look for fields
            foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public))
            {
                var descriptor = new FieldInfoDescriptor(_scriptEngine, field, target);
                yield return(new KeyValuePair <string, PropertyDescriptor>(field.Name, descriptor));
            }
        }