/*=================================GetHashCode================================== **Action: Our algorithm for returning the hashcode is a little bit complex. We look ** for the first non-static field and get it's hashcode. If the type has no ** non-static fields, we return the hashcode of the type. We can't take the ** hashcode of a static member because if that member is of the same type as ** the original type, we'll end up in an infinite loop. **Returns: The hashcode for the type. **Arguments: None. **Exceptions: None. ** ==============================================================================*/ /// <include file='doc\ValueType.uex' path='docs/doc[@for="ValueType.GetHashCode"]/*' /> public override int GetHashCode() { // Note that for correctness, we can't use any field of the value type // since that field may be mutable in some way. If we use that field // and the value changes, we may not be able to look up that type in a // hash table. For correctness, we need to use something unique to // the type of this object. // HOWEVER, we decided that the perf of returning a constant value (such as // the hash code for the type) would be too big of a perf hit. We're willing // to deal with less than perfect results, and people should still be // encouraged to override GetHashCode. BCLDebug.Correctness(!(this is System.Collections.DictionaryEntry), "Calling GetHashCode on DictionaryEntry is dumb and probably wrong."); BCLDebug.Perf(false, "ValueType::GetHashCode is not fast. Perhaps " + this.GetType().FullName + " should override GetHashCode()"); RuntimeType thisType = (RuntimeType)this.GetType(); FieldInfo[] thisFields = thisType.InternalGetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, false); if (thisFields.Length > 0) { for (int i = 0; i < thisFields.Length; i++) { Object obj = ((Object)(((RuntimeFieldInfo)thisFields[i]).InternalGetValue((Object)this, false))); if (obj != null) { return(obj.GetHashCode()); } } } // Using the method table pointer is about 4x faster than getting the // sync block index for the Type object. return(GetMethodTablePtrAsInt(this)); }
// This should ideally become visible only within mscorlib's Assembly. // // Looks up the resource string value for key. // internal static String GetResourceString(String key) { if (SystemResMgr == null) { InitResourceManager(); } String s; // We unfortunately have a somewhat common potential for infinite // loops with mscorlib's ResourceManager. If "potentially dangerous" // code throws an exception, we will get into an infinite loop // inside the ResourceManager and this "potentially dangerous" code. // Potentially dangerous code includes the IO package, CultureInfo, // parts of the loader, some parts of Reflection, Security (including // custom user-written permissions that may parse an XML file at // class load time), assembly load event handlers, etc. Essentially, // this is not a bounded set of code, and we need to fix the problem. // Fortunately, this is limited to mscorlib's error lookups and is NOT // a general problem for all user code using the ResourceManager. // The solution is to make sure only one thread at a time can call // GetResourceString. If the same thread comes into GetResourceString // twice before returning, we're going into an infinite loop and we // should return a bogus string. -- BrianGru, 6/26/2001 // @TODO: This is a quick & easy solution, but may not be optimal. // Note: typeof(Environment) is used elsewhere - don't lock on it. lock (m_resMgrLockObject) { if (m_loadingResource) { // This may be bad, depending on how it happens. BCLDebug.Correctness(false, "Infinite recursion during resource lookup. Resource name: " + key); return("[Resource lookup failed - infinite recursion detected. Resource name: " + key + ']'); } m_loadingResource = true; s = SystemResMgr.GetString(key, null); m_loadingResource = false; } BCLDebug.Assert(s != null, "Managed resource string lookup failed. Was your resource name misspelled? Did you rebuild mscorlib after adding a resource to resources.txt? Debug this w/ cordbg and bug whoever owns the code that called Environment.GetResourceString. Resource name was: \"" + key + "\""); return(s); }