private int DeleteMemberByDispID(int dispid)
            {
                var member = ReflectMembers.Concat(expandoMembers).FirstOrDefault(testMember => testMember.DispID == dispid);

                if (!member.IsDefault && RemoveMember(member.Name))
                {
                    return(HResult.S_OK);
                }

                // IDispatchEx specifies S_FALSE, but DISP_E_UNKNOWNNAME provides parity with .NET Framework CCWs
                return(HResult.DISP_E_UNKNOWNNAME);
            }
            private int GetNextDispID(int dispidCurrent, out int dispidNext)
            {
                var member = ReflectMembers.Concat(expandoMembers).OrderBy(testMember => testMember.DispID).FirstOrDefault(testMember => testMember.DispID > dispidCurrent);

                if (!member.IsDefault)
                {
                    dispidNext = member.DispID;
                    return(HResult.S_OK);
                }

                dispidNext = SpecialDispIDs.Default;
                return(HResult.S_FALSE);
            }
            private int GetMemberName(int dispid, out string name)
            {
                var member = ReflectMembers.Concat(expandoMembers).OrderBy(testMember => testMember.DispID).FirstOrDefault(testMember => testMember.DispID == dispid);

                if (!member.IsDefault)
                {
                    name = member.Name;
                    return(HResult.S_OK);
                }

                name = null;
                return(HResult.S_FALSE);
            }
            private int DeleteMemberByName(string name, DispatchNameFlags nameFlags)
            {
                var nameComparison = nameFlags.HasFlag(DispatchNameFlags.CaseInsensitive) ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

                var member = ReflectMembers.Concat(expandoMembers).FirstOrDefault(testMember => string.Equals(testMember.Name, name, nameComparison));

                if (!member.IsDefault && RemoveMember(member.Name))
                {
                    return(HResult.S_OK);
                }

                // IDispatchEx specifies S_FALSE, but DISP_E_UNKNOWNNAME provides parity with .NET Framework CCWs
                return(HResult.DISP_E_UNKNOWNNAME);
            }
            protected override void AddExpandoMemberName(string name)
            {
                var dispid = 1;

                var dispids = ReflectMembers.Concat(expandoMembers).Select(member => member.DispID).ToArray();

                for (; dispids.Contains(dispid); dispid++)
                {
                    if (dispid >= int.MaxValue)
                    {
                        throw new NotSupportedException("The object cannot support additional dynamic members");
                    }
                }

                base.AddExpandoMemberName(name);
                expandoMembers.Add(new Member {
                    Name = name, DispID = dispid, DispIDName = MiscHelpers.GetDispIDName(dispid)
                });
            }
            private int GetDispID(string name, DispatchNameFlags nameFlags, out int dispid)
            {
                var nameComparison = nameFlags.HasFlag(DispatchNameFlags.CaseInsensitive) ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

                var member = ReflectMembers.Concat(expandoMembers).FirstOrDefault(testMember => string.Equals(testMember.Name, name, nameComparison));

                if (!member.IsDefault)
                {
                    dispid = member.DispID;
                    return(HResult.S_OK);
                }

                if (nameFlags.HasFlag(DispatchNameFlags.Ensure))
                {
                    ThisExpando.AddProperty(name);
                    return(GetDispID(name, nameFlags & ~DispatchNameFlags.Ensure, out dispid));
                }

                dispid = SpecialDispIDs.Unknown;
                return(HResult.DISP_E_UNKNOWNNAME);
            }