Пример #1
0
        private T GetTarget()
        {
            IntPtr h = m_handle;

            // Should only happen for corner cases, like using a
            // WeakReference from a finalizer.
            if (default(IntPtr) == h)
            {
                return(null);
            }

            T target = RuntimeHelpers.UncheckedCast <T>(RuntimeImports.RhHandleGet(h));

            if (target == null)
            {
                target = TryGetComTarget() as T;
            }

            // We want to ensure that the handle was still alive when we fetched the target,
            // so we double check m_handle here. Note that the reading of the handle
            // value has to be volatile for this to work, but reading of m_handle does not.

            if (default(IntPtr) == m_handle)
            {
                return(null);
            }

            return(target);
        }
Пример #2
0
 //
 // (Note: I don't like this member being public but I can't use "internal" since this is shared across two implementation assemblies.)
 //
 // This helper stores the companion RuntimeTypeInfo on behalf of S.R.R.
 //
 //  - The generic method variable RUNTIMETYPEINFO is *always* RuntimeTypeInfo. It's only generic because we can't directly mention that class
 //    from System.Private.CoreLib. You must never instantiate this method over any other type since there's only one underlying storage slot.
 //
 public RUNTIMETYPEINFO InternalGetLatchedRuntimeTypeInfo <RUNTIMETYPEINFO>(Func <RuntimeType, RUNTIMETYPEINFO> factory) where RUNTIMETYPEINFO : class
 {
     if (_lazyRuntimeTypeInfo == null)
     {
         // Note that it's possible for Type to not have a TypeInfo, in which case, _lazyRuntimeTypeInfo remains null
         // and this "one-time" initialization will in fact run every time. The assumption is that it's better to throw
         // the null case under the bus in exchange for not introducing an extra indirection for the cases where
         // a TypeInfo actually exists.
         _lazyRuntimeTypeInfo = factory(this);
     }
     return(RuntimeHelpers.UncheckedCast <RUNTIMETYPEINFO>(_lazyRuntimeTypeInfo));
 }
Пример #3
0
        private static Lock GetLock(Object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            // TODO: Fix framework code to not call Monitor methods on Lock objects and replace this check with an assertion.
            // The Lock class is sealed and never cloned, therefore it is safe to use raw pointer equality.
            if (obj.EETypePtr.RawValue == EETypePtr.EETypePtrOf <Lock>().RawValue)
            {
                return(RuntimeHelpers.UncheckedCast <Lock>(obj));
            }

            return(s_lockTable.GetValue(obj, s_createLock));
        }
Пример #4
0
 public static T UncheckedCast <T>(object obj) where T : class
 {
     return(RuntimeHelpers.UncheckedCast <T>(obj));
 }