/// <summary>
 /// Dynamic method to locate the specific properties.
 /// </summary>
 /// <param name="binder">The member binder</param>
 /// <param name="result">The out result</param>
 /// <returns>Returns true if located a value</returns>
 public override bool TryGetMember(GetMemberBinder binder, out object result)
 {
     // if we don't have a predicate that matches then we return null and true;
     var val = _dataObject.GetPropertyValues(GetPredicateUri(binder.Name));
     result = new DynamicCollection(val.Select(x => x is IDataObject ? new BrightstarDynamicObject(x as IDataObject) : x));
     return true;
 }
        public void Add()
        {
            DynamicCollection<char> collection = new DynamicCollection<char>();
            Assert.AreEqual(collection.Length(), 10);

            for (int i = 0; i < 20; i++)
            {
                collection.Add((char)i);
            }

            Assert.AreEqual(collection.Length(), 20);
        }
Esempio n. 3
0
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject" /> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject" /> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result" />.</param>
        /// <returns>true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)</returns>
        /// <exception cref="System.NotSupportedException">No field of Sitecore info matches the name {0} for item {1}.Formatted(name, _item.Paths.FullPath)</exception>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            string name = binder.Name;

            if (name == "Id")
            {
                result = _item.ID;
                return true;
            }


            if (_item.Fields[name] != null)
            {
                result = GetField(name, _item);
               return true;
            }

            SitecoreInfoType infoType;

            if (Enum.TryParse<SitecoreInfoType>(name, out infoType))
            {
                var mapper = new SitecoreInfoMapper();
                var config = new SitecoreInfoConfiguration();
                config.Type = infoType;

                mapper.Setup(new DataMapperResolverArgs(null, config ));
                result = mapper.MapToProperty(new SitecoreDataMappingContext(null, _item, null));
                return true;
            }

           

            switch (name)
            {
                case "Parent":
                    result = CreateNew(_item.Parent);
                    break;
                case "Children":
                    result = new DynamicCollection<DynamicItem>(_item.Children.Select(x => CreateNew(x)).ToArray());
                    break;
            }
            if (result != null) return true;
            
            throw new NotSupportedException("No field of Sitecore info matches the name {0} for item {1}".Formatted(name, _item.Paths.FullPath));

        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;

            string name = binder.Name;


            if (_item.Fields[name] != null)
            {
                FieldRenderer render = new FieldRenderer();
                render.FieldName = name;
                render.Item = _item;

                result = render.Render();
                return true;
            }

            SitecoreInfoType infoType;

            if (Enum.TryParse<SitecoreInfoType>(name, out infoType))
            {
                result = SitecoreInfoHandler.GetItemInfo(infoType, _item, null);
                return true;
            }


            switch (name)
            {
                case "Parent":
                    result = new DynamicItem(_item.Parent);
                    break;
                case "Children":
                    result = new DynamicCollection<DynamicItem>(_item.Children.Select(x => new DynamicItem(x)).ToArray());
                    break;
            }
            if (result != null) return true;
            
            throw new NotSupportedException("No field of Sitecore info matches the name {0} for item {1}".Formatted(name, _item.Paths.FullPath));

        }