コード例 #1
0
 /// <summary>
 /// Authenticates the specified connection.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="collection">The collection.</param>
 /// <param name="username">The username.</param>
 /// <param name="nonce">The nonce.</param>
 /// <param name="key">The key.</param>
 /// <returns></returns>
 public static bool authenticate(this IDBConnection connection, IDBCollection collection, string username, string nonce, SecureString key)
 {
     DBQuery cmd = BuildQuery_authenticate(username, nonce, key);
     IDocument result = connection.Call<Document>(new Query(new DBCursorOptions(collection, selector: cmd, limit: 1))).Documents.FirstOrDefault();
     DBError err;
     return !result.WasError(out err);
 }
コード例 #2
0
		public static void LoadPropertyValue(this ILGenerator il, PropertyInfo property, LocalBuilder target) {
			if (target.LocalType.IsValueType || target.LocalType.IsNullable()) {
				il.LoadLocalAddress(target);
				il.Call(property.GetGetMethod());
			} else {
				il.LoadLocal(target);
				il.CallVirt(property.GetGetMethod());
			}
		}
コード例 #3
0
 public static string asString(this AndroidJavaObject obj)
 {
     using (var stringObj = obj.Call<AndroidJavaObject>("toString")) {
     using (
       var bytesObj = stringObj.Call<AndroidJavaObject>("getBytes", "UTF8")
     ) {
       var bytes = bytesObj.asBytes();
       return bytes == null
     ? null : System.Text.Encoding.UTF8.GetString(bytes);
     }
       }
 }
コード例 #4
0
ファイル: Util.cs プロジェクト: RoyAwesome/Watertight-Engine
 public static object[] SafeCall(this NLua.LuaFunction func, params object[] param)
 {
     try
     {
         return func.Call(param);
     }
     catch (NLua.Exceptions.LuaScriptException e)
     {
         Msg("[Error] " + e.Message);
         return null;
     }
 }
コード例 #5
0
 /// <summary>
 /// Calls a method on a java object while handling null return values.
 /// Sadly, it appears that calling a method that returns a null Object in Java so we work
 /// around this by catching null pointer exceptions a checking for the word "null".
 /// </summary>
 internal static AndroidJavaObject NullSafeCall(this AndroidJavaObject target,
     string methodName, params object[] args) {
     try {
         return target.Call<AndroidJavaObject>(methodName, args);
     } catch (Exception ex) {
         if (ex.Message.Contains("null")) {
             // expected -- means method returned null
             return null;
         } else {
             GooglePlayGames.OurUtils.Logger.w("CallObjectMethod exception: " + ex);
             return null;
         }
     }
 }
コード例 #6
0
ファイル: Solver.cs プロジェクト: Logicalshift/Reason
        /// <summary>
        /// Queries a solver for a goal
        /// </summary>
        public static IQueryResult Query(this ISolver solver, ILiteral goal)
        {
            // Result is false for something without a key
            if (goal.UnificationKey == null)
            {
                return new BasicQueryResult(false, new EmptyBinding(null));
            }

            // Compile the query
            var unifier = new SimpleUnifier();
            var assignments = new PredicateAssignmentList();

            // Assume we have a basic predicate
            foreach (var arg in goal.Dependencies)
            {
                assignments.AddArgument(arg);
            }

            // Run through the unifier
            var freeVariableNames = unifier.QueryUnifier.Bind(assignments.Assignments);
            var freeVariables = freeVariableNames.Select(variable => unifier.GetVariableLocation(variable).Dereference());
            unifier.QueryUnifier.Compile(assignments.Assignments);

            // Call via the solver
            var moveNext = solver.Call(goal.UnificationKey, unifier.GetArgumentVariables(assignments.CountArguments()));

            Func<IQueryResult> nextResult = () =>
                {
                    // Update the variables to the next result
                    var succeeded = moveNext();

                    // Nothing to do if we didn't succeed
                    if (!succeeded)
                    {
                        return new BasicQueryResult(false, new EmptyBinding(null));
                    }

                    // Bind the variables
                    var variableValues = freeVariables
                        .Select(varRef => varRef.Freeze())
                        .Zip(freeVariableNames, (value, name) => new { Value = value, Name = name }).ToArray();
                    var binding = new BasicBinding(null, variableValues.ToDictionary(val => val.Name, val => val.Value));

                    // Return the result
                    return new BasicQueryResult(true, binding);
                };

            // Chain to produce the final result
            return new ChainedResult(nextResult(), () => Task.FromResult(nextResult()));
        }
コード例 #7
0
 /// <summary>
 /// Emits a call to output a textual diagnostic message.
 /// </summary>
 /// <param name="builder">algorithm builder</param>
 /// <param name="args">output arguments</param>
 public static void ReportLine(this IAlgorithmBuilder builder, params object[] args)
 {
     var arg = Expression.Concat(args.Select(_ => ToExpression(_)).ToArray());
     builder.Call(IntrinsicFunctions.ReportLine(arg), arg);
 }
コード例 #8
0
ファイル: Nonce.cs プロジェクト: automatonic/mongodb-net
 /// <summary>
 /// Gets a nonce for subsequent communication
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="cmdCollection">The CMD collection.</param>
 /// <returns></returns>
 public static string nonce(this IDBConnection connection, IDBCollection cmdCollection)
 {
     IDocument result = connection.Call<Document>(new Query(new DBCursorOptions(cmdCollection, selector: _getnonce, limit: 1))).Documents.FirstOrDefault();
     result.ThrowIfResponseNotOK("unable to get nonce value for authentication");
     return result.GetAsString("nonce");
 }
コード例 #9
0
 public static CodeExpression Call(
     this CodeExpression targetObject,
     String methodName
 )
     => targetObject.Call(methodName, new Type[0], new CodeExpression[0]);
コード例 #10
0
ファイル: TypeExtensions.cs プロジェクト: kb10uy/Kecaknoah
 /// <summary>
 /// ラムダ式などで<see cref="Predicate{T}"/>相当のオブジェクトが渡されたものとしてCallします。
 /// </summary>
 /// <param name="obj">対象</param>
 /// <param name="ctx">現在の<see cref="KecaknoahContext"/></param>
 /// <param name="tr">渡すオブジェクト</param>
 /// <returns></returns>
 public static bool CallAsPredicate(this KecaknoahObject obj, KecaknoahContext ctx, KecaknoahObject tr) => obj.Call(ctx, new[] { tr }).ReturningObject.ToBoolean();
コード例 #11
0
ファイル: Function.cs プロジェクト: tormaroe/mist
 public static Expression Call(this Function f, params Expression[] exprs)
 {
     return f.Call(exprs.AsEnumerable());
 }
コード例 #12
0
 public static CodeExpression Call(
     this CodeExpression targetObject,
     String methodName,
     params CodeExpression[] parameters
 )
     => targetObject.Call(methodName, new Type[0], parameters);
コード例 #13
0
ファイル: Function.cs プロジェクト: tormaroe/mist
 public static Expression Call(this Function f, Expression expr)
 {
     return f.Call(expr.AsEnumerable());
 }
コード例 #14
0
        public static CodeIterationStatement ForEach(this CodeParameterDeclarationExpression enumerableVar, Type itemType, out CodeVariableDeclarationStatement item)
        {
            //IEnumerator<itemType> iter = enumerableVar.GetEnumerator()
            var iter = "iter".DeclareGenericVar(itemType, enumerableVar.Call("GetEnumerator"), typeof(IEnumerator<>));

            var iterStatement = new CodeIterationStatement {
                InitStatement = iter,
                TestExpression = iter.Call("MoveNext"),
                IncrementStatement = new CodeSnippetStatement(""),
            };

            item = itemType.DeclareVar("item", iter.RefProperty("Current"));
            iterStatement.Statements.Add(item);
            return iterStatement;
        }
コード例 #15
0
 public static CodeExpression Call(
     this CodeExpression targetObject,
     String methodName,
     params Type[] typeParameters
 )
     => targetObject.Call(methodName, typeParameters, new CodeExpression[0]);
コード例 #16
0
        /// <summary>
        /// Emit Equality Compare for 2 arguments in top of stack
        /// </summary>
        /// <param name="il"></param>
        /// <param name="argType"></param>
        /// <returns></returns>
        public static ILGenerator IsEquals(this ILGenerator il, Type argType)
        {
            if (argType.IsPrimitive)
            {
                return il.Ceq();
            }

            var nulType = Nullable.GetUnderlyingType(argType);
            if (nulType != null)
            {
                var miGetValue = argType.GetProperty("Value", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).GetGetMethod();
                var miHasValue = argType.GetProperty("HasValue", BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly).GetGetMethod();
                var locVar1 = il.DeclareLocal(argType);
                var locVar2 = il.DeclareLocal(argType);
                var lblHasValues = il.DefineLabel();
                var lblExit = il.DefineLabel();

                il
                    .Stloc(locVar2)
                    .Stloc(locVar1)
                    .Ldloca(locVar1)
                    .Call(miHasValue)
                    .Ldloca(locVar2)
                    .Call(miHasValue)
                    .Add()
                    .Dup()
                    .Ldc_I4(2)
                    .Beq(lblHasValues)
                    .Ldtrue()
                    .Xor()
                    .Br(lblExit)
                    .Label(lblHasValues)
                    .Pop()
                    .Ldloca(locVar1)
                    .Call(miGetValue)
                    .Ldloca(locVar2)
                    .Call(miGetValue)
                    .IsEquals(nulType)
                    .Label(lblExit);
                return il;
            }

            var eqMethod = argType.GetMethod("op_Equality",
                BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly,
                null, new Type[] { argType, argType }, null);
            if (eqMethod != null)
            {
                return il.Call(eqMethod);
            }
            var eqType = typeof(IEquatable<>).MakeGenericType(argType);
            var lblEnd = il.DefineLabel();
            if (!argType.IsValueType)
            {
                if (eqType.IsAssignableFrom(argType))
                {
                    var lblHasValue1 = il.DefineLabel();
                    var locVar2 = il.DeclareLocal(argType);

                    il
                        .Stloc(locVar2)
                        .Dup()
                        .Brtrue(lblHasValue1)
                        .Ldloc(locVar2) //                  if arg1 == 0
                        .Ceq() //                           Сравниваем refernces
                        .Br(lblEnd)
                        .Label(lblHasValue1)
                        .Ldloc(locVar2)
                        .Callvirt(eqType.GetMethod("Equals"));
                }
                else
                {
                    il.Ceq();
                }
            }
            else
            {
                if (eqType.IsAssignableFrom(argType))
                {
                    var var1 = il.DeclareLocal(argType);
                    il.Stloc(var1).Ldloca(var1);
                    il.Callvirt(eqType.GetMethod("Equals"));
                }
            }

            il.MarkLabel(lblEnd);
            return il;
        }
コード例 #17
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="il"></param>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        /// <returns></returns>
        public static ILGenerator Convert(this ILGenerator il, Type source, Type destination)
        {
            if (source == null) throw new ArgumentNullException("source");
            if (destination == null) throw new ArgumentNullException("destination");

            if (source == destination) return il;

            if (source == typeof(Object) && destination.IsValueType) return il.Unbox_Any(destination);
            if (source.IsValueType && destination == typeof(Object)) return il.Box(destination);

            // if (source.IsAssignableFrom(destination)) return this;
            // --> it doesn't work for int? -> int, cause int is assignable from int?

            var converter = LookUpConverter(source, destination);
            if (converter != null) // not so beauty, but it's enough for internal code
            {
                // todo. implement invariant culture here
                if (converter is ConstructorInfo) return il.Newobj((ConstructorInfo)converter);
                // note the ClassCastException expected below in near future :)
                return converter.IsVirtual ? il.Callvirt((MethodInfo)converter) : il.Call((MethodInfo)converter);
            }

            Func<ILGenerator, ILGenerator> emitter;
            if (CanGenerateConverter(source, destination, out emitter)) return emitter(il);

            return il.Castclass(destination);
        }