How to use OptionalAttribute class of Telerik.JustMock.AutoMock.Ninject package

Best JustMockLite code snippet using Telerik.JustMock.AutoMock.Ninject.OptionalAttribute

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Target.cs

Source: Target.cs Github

copy
1#region License
2// 
3// Author: Nate Kohari <[email protected]>
4// Copyright (c) 2007-2010, Enkari, Ltd.
5// 
6// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7// See the file LICENSE.txt for details.
8// 
9#endregion
10#region Using Directives
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Reflection;
15using Telerik.JustMock.AutoMock.Ninject.Activation;
16using Telerik.JustMock.AutoMock.Ninject.Infrastructure;
17using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection;
18using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language;
19using Telerik.JustMock.AutoMock.Ninject.Planning.Bindings;
20#endregion
21
22namespace Telerik.JustMock.AutoMock.Ninject.Planning.Targets
23{
24    /// <summary>
25    /// Represents a site on a type where a value can be injected.
26    /// </summary>
27    /// <typeparam name="T">The type of site this represents.</typeparam>
28    public abstract class Target<T> : ITarget
29        where T : ICustomAttributeProvider
30    {
31        private readonly Future<Func<IBindingMetadata, bool>> _constraint;
32        private readonly Future<bool> _isOptional;
33
34        /// <summary>
35        /// Gets the member that contains the target.
36        /// </summary>
37        public MemberInfo Member { get; private set; }
38
39        /// <summary>
40        /// Gets or sets the site (property, parameter, etc.) represented by the target.
41        /// </summary>
42        public T Site { get; private set; }
43
44        /// <summary>
45        /// Gets the name of the target.
46        /// </summary>
47        public abstract string Name { get; }
48
49        /// <summary>
50        /// Gets the type of the target.
51        /// </summary>
52        public abstract Type Type { get; }
53
54        /// <summary>
55        /// Gets the constraint defined on the target.
56        /// </summary>
57        public Func<IBindingMetadata, bool> Constraint
58        {
59            get { return _constraint; }
60        }
61
62        /// <summary>
63        /// Gets a value indicating whether the target represents an optional dependency.
64        /// </summary>
65        public bool IsOptional
66        {
67            get { return _isOptional; }
68        }
69
70        /// <summary>
71        /// Gets a value indicating whether the target has a default value.
72        /// </summary>
73        public virtual bool HasDefaultValue
74        {
75            get { return false; }
76        }
77
78        /// <summary>
79        /// Gets the default value for the target.
80        /// </summary>
81        /// <exception cref="System.InvalidOperationException">If the item does not have a default value.</exception>
82        public virtual object DefaultValue
83        {
84            get { throw new InvalidOperationException(ExceptionFormatter.TargetDoesNotHaveADefaultValue(this)); }
85        }
86
87        /// <summary>
88        /// Initializes a new instance of the Target&lt;T&gt; class.
89        /// </summary>
90        /// <param name="member">The member that contains the target.</param>
91        /// <param name="site">The site represented by the target.</param>
92        protected Target(MemberInfo member, T site)
93        {
94            Ensure.ArgumentNotNull(member, "member");
95            Ensure.ArgumentNotNull(site, "site");
96
97            Member = member;
98            Site = site;
99
100            _constraint = new Future<Func<IBindingMetadata, bool>>(ReadConstraintFromTarget);
101            _isOptional = new Future<bool>(ReadOptionalFromTarget);
102        }
103
104        /// <summary>
105        /// Returns an array of custom attributes of a specified type defined on the target.
106        /// </summary>
107        /// <param name="attributeType">The type of attribute to search for.</param>
108        /// <param name="inherit">Whether to look up the hierarchy chain for inherited custom attributes.</param>
109        /// <returns>An array of custom attributes of the specified type.</returns>
110        public object[] GetCustomAttributes(Type attributeType, bool inherit)
111        {
112            Ensure.ArgumentNotNull(attributeType, "attributeType");
113            return Site.GetCustomAttributesExtended(attributeType, inherit);
114        }
115
116        /// <summary>
117        /// Returns an array of custom attributes defined on the target.
118        /// </summary>
119        /// <param name="inherit">Whether to look up the hierarchy chain for inherited custom attributes.</param>
120        /// <returns>An array of custom attributes.</returns>
121        public object[] GetCustomAttributes(bool inherit)
122        {
123            return Site.GetCustomAttributes(inherit);
124        }
125
126        /// <summary>
127        /// Returns a value indicating whether an attribute of the specified type is defined on the target.
128        /// </summary>
129        /// <param name="attributeType">The type of attribute to search for.</param>
130        /// <param name="inherit">Whether to look up the hierarchy chain for inherited custom attributes.</param>
131        /// <returns><c>True</c> if such an attribute is defined; otherwise <c>false</c>.</returns>
132        public bool IsDefined(Type attributeType, bool inherit)
133        {
134            Ensure.ArgumentNotNull(attributeType, "attributeType");
135            return Site.IsDefined(attributeType, inherit);
136        }
137
138        /// <summary>
139        /// Resolves a value for the target within the specified parent context.
140        /// </summary>
141        /// <param name="parent">The parent context.</param>
142        /// <returns>The resolved value.</returns>
143        public object ResolveWithin(IContext parent)
144        {
145            Ensure.ArgumentNotNull(parent, "parent");
146
147            if (Type.IsArray)
148            {
149                Type service = Type.GetElementType();
150                return GetValues(service, parent).CastSlow(service).ToArraySlow(service);
151            }
152
153            if (Type.IsGenericType)
154            {
155                Type gtd = Type.GetGenericTypeDefinition();
156                Type service = Type.GetGenericArguments()[0];
157
158                if (gtd == typeof(List<>) || gtd == typeof(IList<>) || gtd == typeof(ICollection<>))
159                    return GetValues(service, parent).CastSlow(service).ToListSlow(service);
160
161                if (gtd == typeof(IEnumerable<>))
162                    return GetValues(service, parent).CastSlow(service);
163            }
164
165            return GetValue(Type, parent);
166        }
167
168        /// <summary>
169        /// Gets the value(s) that should be injected into the target.
170        /// </summary>
171        /// <param name="service">The service that the target is requesting.</param>
172        /// <param name="parent">The parent context in which the target is being injected.</param>
173        /// <returns>A series of values that are available for injection.</returns>
174        protected virtual IEnumerable<object> GetValues(Type service, IContext parent)
175        {
176            Ensure.ArgumentNotNull(service, "service");
177            Ensure.ArgumentNotNull(parent, "parent");
178
179            var request = parent.Request.CreateChild(service, parent, this);
180            request.IsOptional = true;
181            return parent.Kernel.Resolve(request);
182        }
183
184        /// <summary>
185        /// Gets the value that should be injected into the target.
186        /// </summary>
187        /// <param name="service">The service that the target is requesting.</param>
188        /// <param name="parent">The parent context in which the target is being injected.</param>
189        /// <returns>The value that is to be injected.</returns>
190        protected virtual object GetValue(Type service, IContext parent)
191        {
192            Ensure.ArgumentNotNull(service, "service");
193            Ensure.ArgumentNotNull(parent, "parent");
194
195            var request = parent.Request.CreateChild(service, parent, this);
196            request.IsUnique = true;
197            return parent.Kernel.Resolve(request).SingleOrDefault();
198        }
199
200        /// <summary>
201        /// Reads whether the target represents an optional dependency.
202        /// </summary>
203        /// <returns><see langword="True"/> if it is optional; otherwise <see langword="false"/>.</returns>
204        protected virtual bool ReadOptionalFromTarget()
205        {
206            return Site.HasAttribute(typeof(OptionalAttribute));
207        }
208
209        /// <summary>
210        /// Reads the resolution constraint from target.
211        /// </summary>
212        /// <returns>The resolution constraint.</returns>
213        protected virtual Func<IBindingMetadata, bool> ReadConstraintFromTarget()
214        {
215            var attributes = this.GetCustomAttributes(typeof(ConstraintAttribute), true) as ConstraintAttribute[];
216
217            if (attributes == null || attributes.Length == 0)
218                return null;
219
220            if (attributes.Length == 1)
221                return attributes[0].Matches;
222
223            return metadata => attributes.All(attribute => attribute.Matches(metadata));
224        }
225    }
226}
Full Screen

OptionalAttribute.cs

Source: OptionalAttribute.cs Github

copy
1#region License
2// 
3// Author: Nate Kohari <[email protected]>
4// Copyright (c) 2007-2010, Enkari, Ltd.
5// 
6// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7// See the file LICENSE.txt for details.
8// 
9#endregion
10#region Using Directives
11using System;
12#endregion
13
14namespace Telerik.JustMock.AutoMock.Ninject
15{
16    /// <summary>
17    /// Indicates that the decorated member represents an optional dependency.
18    /// </summary>
19    [AttributeUsage(AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Parameter,
20        AllowMultiple = false, Inherited = true)]
21    public class OptionalAttribute : Attribute { }
22}
23
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in OptionalAttribute

    No methods found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)