internal static bool HasMember(IDynamicMetaObjectProvider o,
                                string name)
 {
     return(DynamicObjectHelpers.GetMember(o, name) !=
            DynamicObjectHelpers.Sentinel);
     //Alternative impl used when EOs had bug and didn't call fallback ...
     //var mo = o.GetMetaObject(Expression.Parameter(typeof(object), null));
     //foreach (string member in mo.GetDynamicMemberNames()) {
     //    if (string.Equals(member, name, StringComparison.OrdinalIgnoreCase)) {
     //        return true;
     //    }
     //}
     //return false;
 }
Example #2
0
        // VBScriptImport takes the runtime and module as context for the import.
        // It takes a list of names, what, that either identify a (possibly dotted
        // sequence) of names to fetch from Globals or a file name to load.  Names
        // is a list of names to fetch from the final object that what indicates
        // and then set each name in module.  Renames is a list of names to add to
        // module instead of names.  If names is empty, then the name set in
        // module is the last name in what.  If renames is not empty, it must have
        // the same cardinality as names.
        //
        public static object VBScriptImport(VBScript runtime, IDynamicMetaObjectProvider module,
                                            string[] what, string[] names,
                                            string[] renames)
        {
            // Get object or file scope.
            object value = null;

            if (what.Length == 1)
            {
                string name = what[0];
                if (DynamicObjectHelpers.HasMember(runtime.Globals, name))
                {
                    value = DynamicObjectHelpers.GetMember(runtime.Globals, name);
                    // Since runtime.Globals has Sympl's reflection of namespaces and
                    // types, we pick those up first above and don't risk hitting a
                    // NamespaceTracker for assemblies added when we initialized Sympl.
                    // The next check will correctly look up case-INsensitively for
                    // globals the host adds to ScriptRuntime.Globals.
                }
                else if (DynamicObjectHelpers.HasMember(runtime.DlrGlobals, name))
                {
                    value = DynamicObjectHelpers.GetMember(runtime.DlrGlobals, name);
                }
                else
                {
                    throw new ArgumentException(
                              "Import: can't find name in globals -- " + name);
                }
            }
            else
            {
                // What has more than one name, must be Globals access.
                value = runtime.Globals;
                // For more correctness and generality, shouldn't assume all
                // globals are dynamic objects, or that a look up like foo.bar.baz
                // cascades through all dynamic objects.
                // Would need to manually create a CallSite here with Sympl's
                // GetMemberBinder, and think about a caching strategy per name.
                foreach (string name in what)
                {
                    value = DynamicObjectHelpers.GetMember(
                        (IDynamicMetaObjectProvider)value, name);
                }
            }
            // Assign variables in module.
            if (names.Length == 0)
            {
                if (renames.Length == 0)
                {
                    DynamicObjectHelpers.SetMember((IDynamicMetaObjectProvider)module,
                                                   what[what.Length - 1], value);
                }
                else
                {
                    DynamicObjectHelpers.SetMember((IDynamicMetaObjectProvider)module,
                                                   renames[0], value);
                }
            }
            else
            {
                if (renames.Length == 0)
                {
                    renames = names;
                }
                for (int i = 0; i < names.Length; i++)
                {
                    string name   = names[i];
                    string rename = renames[i];
                    DynamicObjectHelpers.SetMember(
                        (IDynamicMetaObjectProvider)module, rename,
                        DynamicObjectHelpers.GetMember(
                            (IDynamicMetaObjectProvider)value, name));
                }
            }
            return(null);
        } // SymplImport