예제 #1
0
        void IDisposable.Dispose()
        {
            foreach (IDisposable element in _tree.Values.ToArray())
            {
                element.Dispose();
            }

            _tree.Clear();
            _parent = null;
            _cls    = null;
        }
예제 #2
0
        public MemberTree PropertyOrField(string name)
        {
            var segments    = name.Split('.');
            var current     = this;
            var currentType = _declaredType;

            foreach (var segment in segments)
            {
                if (current._tree.ContainsKey(segment))
                {
                    current     = current._tree[segment];
                    currentType = current.PropertyOrFieldType;
                }
                else
                {
                    var       property     = currentType.GetProperty(segment, TypeExtensions.BindingFlags);
                    var       declaredType = property?.PropertyType;
                    FieldInfo field        = null;
                    var       isProp       = true;
                    if (property == null)
                    {
                        isProp       = false;
                        field        = currentType.GetField(segment, TypeExtensions.BindingFlags) ?? throw new InvalidOperationException($"The property or field '{name}' does not exist in '{_declaredType.FullName}'");
                        declaredType = field.FieldType;
                    }

                    var element = new MemberTree(declaredType, current)
                    {
                        Field      = field,
                        Property   = property,
                        IsProperty = isProp,
                        Name       = segment
                    };

                    current._tree.Add(segment, element);
                    current     = element;
                    currentType = declaredType;
                }
            }

            return(current);
        }
예제 #3
0
 private MemberTree(Type declaredType, MemberTree parent)
 {
     _declaredType = declaredType;
     _parent       = parent;
 }
예제 #4
0
        public MemberTree PropertyOrFieldOfClass(string name)
        {
            var segments   = name.Split('.');
            var current    = this;
            var currentCls = _cls;
            var propName   = string.Empty;

            for (var i = 0; i < segments.Length; ++i)
            {
                var segment = segments[i];
                if (current._tree.ContainsKey(segment))
                {
                    current    = current._tree[segment];
                    currentCls = current._cls;
                }
                else
                {
                    MemberTree      element;
                    PropertyBuilder property;
                    Class           cls;
                    if (i == segments.Length - 1)
                    {
                        if (!currentCls.PropertyBuilders.ContainsKey(segment))
                        {
                            throw new InvalidOperationException($"The property or field '{name}' does not exist in '{_cls.Name}'");
                        }
                        property = currentCls.PropertyBuilders[segment];
                        cls      = current._cls.SubClasses.ContainsKey(segment) ? current._cls.SubClasses[segment] : current._cls;
                        var type = property.PropertyType;
                        element = new MemberTree(type, current)
                        {
                            _cls       = cls,
                            Property   = property,
                            IsProperty = true,
                            Name       = segment
                        };
                        current._tree.Add(segment, element);
                        current = element;

                        break;
                    }

                    propName = propName == string.Empty ? segment : $"{propName}.{segment}";
                    if (!currentCls.PropertyBuilders.ContainsKey(segment))
                    {
                        throw new InvalidOperationException($"The property or field '{name}' does not exist in '{_cls.Name}'");
                    }
                    cls      = current._cls.SubClasses[segment];
                    property = currentCls.PropertyBuilders[segment];
                    var declaredType = property.PropertyType;
                    element = new MemberTree(declaredType, current)
                    {
                        _cls       = cls,
                        Property   = property,
                        IsProperty = true,
                        Name       = segment
                    };

                    current._tree.Add(segment, element);
                    currentCls = cls;
                    current    = element;
                }
            }

            return(current);
        }