当前位置: 首页>>代码示例>>C#>>正文


C# IContractResolver.ResolveContract方法代码示例

本文整理汇总了C#中IContractResolver.ResolveContract方法的典型用法代码示例。如果您正苦于以下问题:C# IContractResolver.ResolveContract方法的具体用法?C# IContractResolver.ResolveContract怎么用?C# IContractResolver.ResolveContract使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IContractResolver的用法示例。


在下文中一共展示了IContractResolver.ResolveContract方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FindPropertyAndParent

        public static JsonPatchProperty FindPropertyAndParent(
            object targetObject,
            string propertyPath,
            IContractResolver contractResolver)
        {
            try
            {
                var splitPath = propertyPath.Split('/');

                // skip the first one if it's empty
                var startIndex = (string.IsNullOrWhiteSpace(splitPath[0]) ? 1 : 0);

                for (int i = startIndex; i < splitPath.Length; i++)
                {
                    var jsonContract = (JsonObjectContract)contractResolver.ResolveContract(targetObject.GetType());

                    foreach (var property in jsonContract.Properties)
                    {
                        if (string.Equals(property.PropertyName, splitPath[i], StringComparison.OrdinalIgnoreCase))
                        {
                            if (i == (splitPath.Length - 1))
                            {
                                return new JsonPatchProperty(property, targetObject);
                            }
                            else
                            {
                                targetObject = property.ValueProvider.GetValue(targetObject);

                                // if property is of IList type then get the array index from splitPath and get the
                                // object at the indexed position from the list.
                                if (typeof(IList).GetTypeInfo().IsAssignableFrom(property.PropertyType.GetTypeInfo()))
                                {
                                    var index = int.Parse(splitPath[++i]);
                                    targetObject = ((IList)targetObject)[index];
                                }
                            }

                            break;
                        }
                    }
                }

                return null;
            }
            catch (Exception)
            {
                // will result in JsonPatchException in calling class, as expected
                return null;
            }
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:50,代码来源:PropertyHelpers.cs

示例2: ScimPatchObjectAnalysis

        public ScimPatchObjectAnalysis(
            ScimServerConfiguration serverConfiguration,
            object objectToSearch, 
            string filter, 
            IContractResolver contractResolver,
            Operation operation)
        {
            _ServerConfiguration = serverConfiguration;
            _ContractResolver = contractResolver;
            _Operation = operation;
            PatchMembers = new List<PatchMember>();

            /* 
                ScimFilter.cs will handle normalizing the actual path string. 
                
                Examples:
                "path":"members"
                "path":"name.familyName"
                "path":"addresses[type eq \"work\"]"
                "path":"members[value eq \"2819c223-7f76-453a-919d-413861904646\"]"
                "path":"members[value eq \"2819c223-7f76-453a-919d-413861904646\"].displayName"

                Once normalized, associate each resource member with its filter (if present).
                This is represented as a PathMember, which is essentially a tuple of <memberName, memberFilter?>
            */
            var pathTree = new ScimFilter(_ServerConfiguration.ResourceExtensionSchemas.Keys, filter).Paths.ToList();
            var lastPosition = 0;
            var nodes = GetAffectedMembers(pathTree, ref lastPosition, new Node(objectToSearch, null));
            
            if ((pathTree.Count - lastPosition) > 1)
            {
                IsValidPathForAdd = false;
                IsValidPathForRemove = false;
                return;
            }

            foreach (var node in nodes)
            {
                var attribute = node.Target as MultiValuedAttribute;
                JsonProperty attemptedProperty;
                if (attribute != null && PathIsMultiValuedEnumerable(pathTree[pathTree.Count - 1].Path, node, out attemptedProperty))
                {
                    /* Check if we're at a MultiValuedAttribute.
                       If so, then we'll return a special PatchMember.  This is because our actual target is 
                       an element within an enumerable. (e.g. User->Emails[element])
                       So a PatchMember must have three pieces of information: (following the example above)
                       > Parent (User)
                       > PropertyPath (emails)
                       > Actual Target (email instance/element)
                    */

                    UseDynamicLogic = false;
                    IsValidPathForAdd = true;
                    IsValidPathForRemove = true;
                    PatchMembers.Add(
                        new PatchMember(
                            pathTree[pathTree.Count - 1].Path,
                            new JsonPatchProperty(attemptedProperty, node.Parent),
                            node.Target));
                }
                else
                {
                    UseDynamicLogic = false;

                    var jsonContract = (JsonObjectContract)contractResolver.ResolveContract(node.Target.GetType());
                    attemptedProperty = jsonContract.Properties.GetClosestMatchProperty(pathTree[pathTree.Count - 1].Path);
                    if (attemptedProperty == null)
                    {
                        IsValidPathForAdd = false;
                        IsValidPathForRemove = false;
                    }
                    else
                    {
                        IsValidPathForAdd = true;
                        IsValidPathForRemove = true;
                        PatchMembers.Add(
                            new PatchMember(
                                pathTree[pathTree.Count - 1].Path,
                                new JsonPatchProperty(attemptedProperty, node.Target)));
                    }
                }
            }
        }
开发者ID:PowerDMS,项目名称:Owin.Scim,代码行数:83,代码来源:ScimPatchObjectAnalysis.cs

示例3: ObjectTreeAnalysisResult

        public ObjectTreeAnalysisResult(
            object objectToSearch,
            string propertyPath,
            IContractResolver contractResolver)
        {
            // construct the analysis result.

            // split the propertypath, and if necessary, remove the first
            // empty item (that's the case when it starts with a "/")
            var propertyPathTree = propertyPath.Split(
                new char[] { '/' },
                StringSplitOptions.RemoveEmptyEntries);

            // we've now got a split up property tree "base/property/otherproperty/..."
            int lastPosition = 0;
            object targetObject = objectToSearch;
            for (int i = 0; i < propertyPathTree.Length; i++)
            {
                lastPosition = i;

                // if the current target object is an ExpandoObject (IDictionary<string, object>),
                // we cannot use the ContractResolver.
                var dictionary = targetObject as IDictionary<string, object>;
                if (dictionary != null)
                {
                    // find the value in the dictionary
                    if (dictionary.ContainsCaseInsensitiveKey(propertyPathTree[i]))
                    {
                        var possibleNewTargetObject = dictionary.GetValueForCaseInsensitiveKey(propertyPathTree[i]);

                        // unless we're at the last item, we should set the targetobject
                        // to the new object.  If we're at the last item, we need to stop
                        if (i != propertyPathTree.Length - 1)
                        {
                            targetObject = possibleNewTargetObject;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    // if the current part of the path is numeric, this means we're trying
                    // to get the propertyInfo of a specific object in an array.  To allow
                    // for this, the previous value (targetObject) must be an IEnumerable, and
                    // the position must exist.

                    int numericValue = -1;
                    if (int.TryParse(propertyPathTree[i], out numericValue))
                    {
                        var element = GetElementAtFromObject(targetObject, numericValue);
                        if (element != null)
                        {
                            targetObject = element;
                        }
                        else
                        {
                            break;
                        }
                    }
                    else
                    {
                        var jsonContract = (JsonObjectContract)contractResolver.ResolveContract(targetObject.GetType());

                        // does the property exist?
                        var attemptedProperty = jsonContract
                            .Properties
                            .FirstOrDefault(p => string.Equals(p.PropertyName, propertyPathTree[i], StringComparison.OrdinalIgnoreCase));

                        if (attemptedProperty != null)
                        {
                            // unless we're at the last item, we should continue searching.
                            // If we're at the last item, we need to stop
                            if ((i != propertyPathTree.Length - 1))
                            {
                                targetObject = attemptedProperty.ValueProvider.GetValue(targetObject);
                            }
                        }
                        else
                        {
                            // property cannot be found, and we're not working with dynamics.
                            // Stop, and return invalid path.
                            break;
                        }
                    }
                }
            }

            if (propertyPathTree.Length - lastPosition != 1)
            {
                IsValidPathForAdd = false;
                IsValidPathForRemove = false;
                return;
            }

            // two things can happen now.  The targetproperty can be an IDictionary - in that
            // case, it's valid for add if there's 1 item left in the propertyPathTree.
            //
//.........这里部分代码省略.........
开发者ID:aspnet,项目名称:JsonPatch,代码行数:101,代码来源:ObjectTreeAnalyisResult.cs


注:本文中的IContractResolver.ResolveContract方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。