示例#1
0
        /// <summary>
        /// This method gets called once for each event from the Http DiagnosticSource.
        /// </summary>
        /// <param name="value">The pair containing the event name, and an object representing the payload. The payload
        /// is essentially a dynamic object that contain different properties depending on the event.</param>
        public void OnNext(KeyValuePair <string, object> value)
        {
            try
            {
                switch (value.Key)
                {
                case "System.Net.Http.Desktop.HttpRequestOut.Start":
                {
                    var request = (HttpWebRequest)this.requestFetcherRequestEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.HttpDesktopBeginCallbackCalled(ClientServerDependencyTracker.GetIdForRequestObject(request), request.RequestUri.ToString());

                    // With this event, DiagnosticSource injects headers himself (after the event)
                    // ApplicationInsights must not do this
                    this.httpDesktopProcessing.OnBegin(request, false);
                    break;
                }

                case "System.Net.Http.Desktop.HttpRequestOut.Stop":
                {
                    // request is never null
                    var request = this.requestFetcherResponseEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.HttpDesktopEndCallbackCalled(ClientServerDependencyTracker.GetIdForRequestObject(request));
                    var response = this.responseFetcher.Fetch(value.Value);
                    this.httpDesktopProcessing.OnEndResponse(request, response);
                    break;
                }

                case "System.Net.Http.Desktop.HttpRequestOut.Ex.Stop":
                {
                    // request is never null
                    var request = this.requestFetcherResponseExEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.HttpDesktopEndCallbackCalled(ClientServerDependencyTracker.GetIdForRequestObject(request));
                    object statusCode = this.responseExStatusFetcher.Fetch(value.Value);
                    object headers    = this.responseExHeadersFetcher.Fetch(value.Value);
                    this.httpDesktopProcessing.OnEndResponse(request, statusCode, headers);
                    break;
                }

                case "System.Net.Http.InitializationFailed":
                {
                    DependencyTableStore.IsDesktopHttpDiagnosticSourceActivated = false;

                    Exception ex = (Exception)value.Value.GetType().GetProperty("Exception")?.GetValue(value.Value);
                    DependencyCollectorEventSource.Log.HttpHandlerDiagnosticListenerFailedToInitialize(ex?.ToInvariantString());
                    break;
                }

                default:
                {
                    DependencyCollectorEventSource.Log.NotExpectedCallback(value.GetHashCode(), value.Key, "unknown key");
                    break;
                }
                }
            }
            catch (Exception exc)
            {
                DependencyCollectorEventSource.Log.CallbackError(0, "OnNext", exc);
            }
        }
        public void CanGenerateAHashCode()
        {
            var kvp1 = new KeyValuePair <string, string> {
                Key   = "K1",
                Value = "KVP 1"
            };

            Assert.True(kvp1.GetHashCode() != default);
        }
示例#3
0
    public static void Main()
    {
        var a1 = new KeyValuePair <int, int>(1, 2);
        var a2 = new KeyValuePair <int, int>(1, 3);

        Console.WriteLine(a1.GetHashCode() == a2.GetHashCode());
        var b1 = new KeyValuePair <int, string>(1, "x");
        var b2 = new KeyValuePair <int, string>(1, "y");

        Console.WriteLine(b1.GetHashCode() == b2.GetHashCode());
    }
示例#4
0
        /// <summary>
        /// This method gets called once for each event from the Http DiagnosticSource.
        /// </summary>
        /// <param name="value">The pair containing the event name, and an object representing the payload. The payload
        /// is essentially a dynamic object that contain different properties depending on the event.</param>
        public void OnNext(KeyValuePair <string, object> value)
        {
            try
            {
                switch (value.Key)
                {
                case "System.Net.Http.Desktop.HttpRequestOut.Start":
                {
                    var request = (HttpWebRequest)this.requestFetcherRequestEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.BeginCallbackCalled(request.GetHashCode(), value.Key);

                    // With this event, DiagnosticSource injects headers himself (after the event)
                    // ApplicationInsights must not do this
                    this.httpDesktopProcessing.OnBegin(request, false);
                    break;
                }

                // remove "System.Net.Http.Request" in 2.5.0 (but keep the same code for "System.Net.Http.Desktop.HttpRequestOut.Start")
                // event was temporarily introduced in DiagnosticSource and removed before stable release
                case "System.Net.Http.Request":
                {
                    // request is never null
                    var request = (HttpWebRequest)this.requestFetcherRequestEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.BeginCallbackCalled(request.GetHashCode(), value.Key);
                    this.httpDesktopProcessing.OnBegin(request);
                    break;
                }

                case "System.Net.Http.Desktop.HttpRequestOut.Stop":

                // remove "System.Net.Http.Response" in 2.5.0 (but keep the same code for "System.Net.Http.Desktop.HttpRequestOut.Stop")
                // event was temporarily introduced in DiagnosticSource and removed before stable release
                case "System.Net.Http.Response":
                {
                    // request is never null
                    var request = (HttpWebRequest)this.requestFetcherResponseEvent.Fetch(value.Value);
                    DependencyCollectorEventSource.Log.EndCallbackCalled(request.GetHashCode().ToString(CultureInfo.InvariantCulture));
                    var response = (HttpWebResponse)this.responseFetcher.Fetch(value.Value);
                    this.httpDesktopProcessing.OnEnd(null, request, response);
                    break;
                }

                default:
                {
                    DependencyCollectorEventSource.Log.NotExpectedCallback(value.GetHashCode(), value.Key, "unknown key");
                    break;
                }
                }
            }
            catch (Exception exc)
            {
                DependencyCollectorEventSource.Log.CallbackError(0, "OnNext", exc);
            }
        }
示例#5
0
        public void GetHashCode_DefaultPairs_EqualHashCodes()
        {
            // Arrange
            var kvp1 = new KeyValuePair <int, string>();
            var kvp2 = new KeyValuePair <int, string>();

            // Act
            var hashCode1 = kvp1.GetHashCode();
            var hashCode2 = kvp2.GetHashCode();

            // Assert
            Assert.That(hashCode1, Is.EqualTo(hashCode2));
        }
示例#6
0
        static void Main(string[] args)
        {
            Some o1 = new Some();
            Some o2 = new Some();

            Console.WriteLine(o1.GetHashCode());
            Console.WriteLine(o2.GetHashCode());

            string s1 = "aaa";
            string s2 = "aaa";

            Console.WriteLine(s1.GetHashCode());
            Console.WriteLine(s2.GetHashCode());

            // key value struct
            Console.WriteLine(new string('-', 80));

            var a = new KeyValuePair <int, string>(10, "aaa");
            var b = new KeyValuePair <int, string>(10, "bbb");

            Console.WriteLine(a.GetHashCode());
            Console.WriteLine(b.GetHashCode());

            Console.WriteLine(new string('-', 80));

            // Для структури перевизначено методи Equals + GetHashCode
            // також реалізовано інтерфейс IEquatable
            Some some = new Some();
            Str  str1 = new Str(10, 1212, "hello", some);
            Str  str2 = new Str(10, 12, "hello", some);

            object oo1 = str1;
            object oo2 = str2;

            Console.WriteLine(str1.Equals(oo1));

            Console.WriteLine(str1.GetHashCode() + " : " + oo1.GetHashCode() + " : " + ((Str)oo1).GetHashCode());
            Console.WriteLine(str2.GetHashCode() + " : " + oo2.GetHashCode() + " : " + ((Str)oo2).GetHashCode());
            Console.WriteLine(str2.SomeHash());

            Console.WriteLine(new string('-', 80));
            // Різниця в використанні метода екземпляра чи метод статичного в тому що статичний
            // не кине ексепшин при null строки або строк
            string ss1 = null;
            string ss2 = null;

            //Console.WriteLine(ss1.Equals(ss2));
            Console.WriteLine(string.Equals(ss1, ss2));

            Console.ReadLine();
        }
示例#7
0
        public override int GetHashCode()
        {
            int        hashCode = 1;
            Enumerator e        = GetEnumerator();

            while (e.MoveNext())
            {
                KeyValuePair <K, V> pair = e.Current;

                hashCode = 31 * hashCode + pair.GetHashCode();
            }

            return(hashCode);
        }
示例#8
0
        public void GetHashCode_EqualPairs_EqualHashCodes()
        {
            // Arrange
            var key   = Random.Next();
            var value = Random.GetString(10);
            var kvp1  = new KeyValuePair <int, string>(key, value);
            var kvp2  = new KeyValuePair <int, string>(key, value);

            // Act
            var hashCode1 = kvp1.GetHashCode();
            var hashCode2 = kvp2.GetHashCode();

            // Assert
            Assert.That(hashCode1, Is.EqualTo(hashCode2));
        }
示例#9
0
        public virtual void TestEntryType()
        {
            Ref a = NewRef("refs/heads/A", ID_ONE);
            Ref b = NewRef("refs/heads/B", ID_TWO);

            packed = ToList(a, b);
            RefMap map = new RefMap("refs/heads/", packed, loose, resolved);
            Iterator <KeyValuePair <string, Ref> > itr   = map.EntrySet().Iterator();
            KeyValuePair <string, Ref>             ent_a = itr.Next();
            KeyValuePair <string, Ref>             ent_b = itr.Next();

            NUnit.Framework.Assert.AreEqual(ent_a.GetHashCode(), "A".GetHashCode());
            NUnit.Framework.Assert.AreEqual(ent_a, ent_a);
            NUnit.Framework.Assert.IsFalse(ent_a.Equals(ent_b));
            NUnit.Framework.Assert.AreEqual(a.ToString(), ent_a.ToString());
        }
        public int GetIndexOfAgent(KeyValuePair <AgentStateStatic, AgentStateDynamic> agent)
        {
            int index = -1;

            foreach (var a in agents)
            {
                index++;
                if (a.GetHashCode() == agent.GetHashCode())
                {
                    return(index);
                }
            }

            index = -1;
            return(index);
        }
示例#11
0
        public void Add(KeyValuePair <KeyObject, object> item)
        {
            var index = Math.Abs(item.GetHashCode()) % _inner.Length;
            var list  = _inner[index];

            if (list == null)
            {
                list          = new List <KeyValuePair <KeyObject, object> >(capasity);
                _inner[index] = list;
            }

            if (list.Any(x => x.Key.Equals(item.Key)))
            {
                throw new ArgumentException($"The key \"{item.Key}\" already exists in the collection");
            }

            list.Add(item);
        }
示例#12
0
        public void KeyvaluePairEqual()
        {
            var KeyValuePairA = new KeyValuePair <string, Type>("KeyValuePair", typeof(UnitTest1));
            var KeyValuePairB = new KeyValuePair <string, Type>("KeyValuePair", typeof(UnitTest1));

            Assert.Equal(KeyValuePairA, KeyValuePairB);
            Assert.Equal(KeyValuePairA.GetHashCode(), KeyValuePairB.GetHashCode());



            var dicKeyValues = new ConcurrentDictionary <KeyValuePair <string, Type>, string>();


            var lazyKeyValuePairA = dicKeyValues.GetOrAdd(KeyValuePairA, (o) => "KeyValuePairA");
            var lazyKeyValuePairB = dicKeyValues.GetOrAdd(KeyValuePairA, (o) => "KeyValuePairB");

            Assert.Equal(lazyKeyValuePairA, lazyKeyValuePairB);
        }
                public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
                {
                    result = null;

                    switch (binder.Name)
                    {
                    case "Equals":
                        if (args.Length == 1)
                        {
                            result = Wrapped.Equals(args[0]);
                            return(true);
                        }
                        return(false);

                    case "GetHashCode":
                        if (args.Length == 0)
                        {
                            result = Wrapped.GetHashCode();
                            return(true);
                        }
                        return(false);

                    case "GetType":
                        if (args.Length == 0)
                        {
                            // Do *not* proxy this call, but *do* respond to it
                            result = this.GetType();
                            return(true);
                        }
                        return(false);

                    case "ToString":
                        if (args.Length == 0)
                        {
                            result = Wrapped.ToString();
                            return(true);
                        }

                        return(false);

                    default:
                        return(false);
                    }
                }
示例#14
0
        public virtual void TestEquality()
        {
            var l1 = new List <string>()
            {
                "1", "2"
            };
            var l2 = new List <string>()
            {
                "1", "2"
            };

            Assert.False(l1.Equals(l2));
            Assert.True(l1.SequenceEqual(l2));
            Assert.NotEqual(l1.GetHashCode(), l2.GetHashCode());
            Assert.Equal(l1.HashCode(), l2.HashCode());

            var k1 = new Dictionary <string, string>()
            {
                { "1", "2" }
            };
            var k2 = new Dictionary <string, string>()
            {
                { "1", "2" }
            };

            Assert.False(k1.Equals(k2));
            Assert.True(k1.SequenceEqual(k2));
            Assert.NotEqual(k1.GetHashCode(), k2.GetHashCode());
            Assert.Equal(k1.HashCode(), k2.HashCode());

            var p1 = new KeyValuePair <string, string>("1", "2");
            var p2 = new KeyValuePair <string, string>("1", "2");

            Assert.True(p1.Equals(p2));
            Assert.Equal(p1.GetHashCode(), p2.GetHashCode());

            List <string> l3 = null;

            Assert.False(l1.Equal(l3));
            Assert.False(l3.Equal(l1));
            Assert.True(l3.Equal(l3));
        }
示例#15
0
        public void GetHashCode_CompareMaps_MapsAreEqual()
        {
            // Arrange
            var mapFromIntToFloat = new Map(typeof(int), typeof(float));
            var mapStringToSource = new Map(typeof(string), typeof(Source));

            var intToFloatPair          = new KeyValuePair <Type, Type>(typeof(int), typeof(float));
            var stringToSourcePair      = new KeyValuePair <Type, Type>(typeof(string), typeof(Source));
            var sourceToDestinationPair = new KeyValuePair <Type, Type>(typeof(Source), typeof(Destination));

            // Act
            var intToFloatHashCode          = intToFloatPair.GetHashCode();
            var stringToSourceHashCode      = stringToSourcePair.GetHashCode();
            var sourceToDestinationHashCode = sourceToDestinationPair.GetHashCode();

            var mapIntToFloatHashCode          = mapFromIntToFloat.GetHashCode();
            var mapStringToSourceHashCode      = mapStringToSource.GetHashCode();
            var mapSourceToDestinationHashCode = this.map.GetHashCode();

            // Assert
            Assert.AreEqual(intToFloatHashCode, mapIntToFloatHashCode);
            Assert.AreEqual(stringToSourceHashCode, mapStringToSourceHashCode);
            Assert.AreEqual(sourceToDestinationHashCode, mapSourceToDestinationHashCode);
        }
示例#16
0
 public int GetHashCode(KeyValuePair <String, Object> obj)
 {
     return(obj.GetHashCode());
 }
示例#17
0
 public int GetHashCode(KeyValuePair <string, string> obj)
 {
     return(obj.GetHashCode() + obj.Value.GetHashCode());
 }
 public int GetHashCode(KeyValuePair <string, List <string> > obj)
 {
     return(obj.GetHashCode());
 }
示例#19
0
 public int GetHashCode(KeyValuePair <UniqueAddress, ImmutableHashSet <UniqueAddress> > obj)
 {
     return(obj.GetHashCode());
 }
示例#20
0
文件: Mapper.cs 项目: yt2100/microfx
 public int GetHashCode(KeyValuePair <string, PropertyInfo> obj)
 {
     return(obj.GetHashCode());
 }
示例#21
0
 public override int GetHashCode(KeyValuePair <T, V> obj)
 {
     return(obj.GetHashCode());
 }
 /// <summary>
 ///   Returns a hash code for the specified object.
 /// </summary>
 /// <param name="obj"> The <see cref="T:System.Object"></see> for which a hash code is to be returned. </param>
 /// <returns> A hash code for the specified object. </returns>
 /// <exception cref="T:System.ArgumentNullException">The type of obj is a reference type and obj is null.</exception>
 public int GetHashCode(KeyValuePair <TKey, TValue> obj)
 {
     return(GetHashCode() ^ obj.GetHashCode());
 }
示例#23
0
 public int GetHashCode(KeyValuePair <string, string> obj) => obj.GetHashCode();
示例#24
0
 public override int GetHashCode(KeyValuePair <string, string> obj)
 {
     return(obj.GetHashCode());
 }
示例#25
0
 /// <summary>
 ///     Returns a hash code for this instance.
 /// </summary>
 /// <returns>
 ///     A hash code for this instance, suitable for use in hashing algorithms and data
 ///     structures like a hash table.
 /// </returns>
 public override int GetHashCode()
 {
     return(_keyValuePair.GetHashCode());
 }
示例#26
0
 /// <summary>
 /// Возвращает хэш-код указанного объекта.
 /// </summary>
 /// <returns>
 /// Хэш-код указанного объекта.
 /// </returns>
 /// <param name="obj">Объект <see cref="T:System.Object"/>, для которого должен быть возвращен хэш-код.</param><exception cref="T:System.ArgumentNullException">Тип <paramref name="obj"/> является ссылочным типом, значение параметра <paramref name="obj"/> — null.</exception>
 public int GetHashCode(KeyValuePair <char, char> obj)
 {
     /*int hashCode = obj.Key.GetHashCode() ^ obj.Value.GetHashCode();
      * return hashCode.GetHashCode();*/
     return(obj.GetHashCode());
 }
示例#27
0
 public int GetHashCode(KeyValuePair <TKey, TValue> x) => x.GetHashCode();
示例#28
0
 public int GetHashCode(KeyValuePair <APC, string> obj)
 {
     return(obj.GetHashCode());
 }
示例#29
0
 public int GetHashCode(KeyValuePair <K, V> obj)
 {
     return(obj.GetHashCode());
 }
示例#30
0
 /// <summary>
 /// Returns a hash code for the specified object.
 /// </summary>
 /// <param name="obj">The <see cref="object" /> for which a hash code is to be returned.</param>
 /// <returns>A hash code for the specified object.</returns>
 public Int32 GetHashCode(KeyValuePair <TKey, TValue> obj)
 {
     return(obj.GetHashCode());
 }