{
public virtual object Clone() => MemberwiseClone();
- public static Delegate? Combine(Delegate? a, Delegate? b)
+ public static Delegate? Combine(Delegate? a, Delegate? b) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
{
if (a is null)
return b;
return a.CombineImpl(b);
}
- public static Delegate? Combine(params Delegate?[]? delegates)
+ public static Delegate? Combine(params Delegate?[]? delegates) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
{
if (delegates == null || delegates.Length == 0)
return null;
~Gen2GcCallback()
{
// Check to see if the target object is still alive.
- object targetObj = _weakTargetObj.Target;
+ object? targetObj = _weakTargetObj.Target;
if (targetObj == null)
{
// The target object is dead, so this callback object is no longer needed.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
private IntPtr _handle;
// Allocate a handle storing the object and the type.
- private GCHandle(object value, GCHandleType type)
+ private GCHandle(object? value, GCHandleType type)
{
// Make sure the type parameter is within the valid range for the enum.
if ((uint)type > (uint)GCHandleType.Pinned) // IMPORTANT: This must be kept in sync with the GCHandleType enum.
/// <summary>Creates a new GC handle for an object.</summary>
/// <param name="value">The object that the GC handle is created for.</param>
/// <returns>A new GC handle that protects the object.</returns>
- public static GCHandle Alloc(object value) => new GCHandle(value, GCHandleType.Normal);
+ public static GCHandle Alloc(object? value) => new GCHandle(value, GCHandleType.Normal);
/// <summary>Creates a new GC handle for an object.</summary>
/// <param name="value">The object that the GC handle is created for.</param>
/// <param name="type">The type of GC handle to create.</param>
/// <returns>A new GC handle that protects the object.</returns>
- public static GCHandle Alloc(object value, GCHandleType type) => new GCHandle(value, type);
+ public static GCHandle Alloc(object? value, GCHandleType type) => new GCHandle(value, type);
/// <summary>Frees a GC handle.</summary>
public void Free()
}
// Target property - allows getting / updating of the handle's referent.
- public object Target
+ public object? Target
{
get
{
public override int GetHashCode() => _handle.GetHashCode();
- public override bool Equals(object o) => o is GCHandle && _handle == ((GCHandle)o)._handle;
+ public override bool Equals(object? o) => o is GCHandle && _handle == ((GCHandle)o)._handle;
public static bool operator ==(GCHandle a, GCHandle b) => a._handle == b._handle;
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-namespace System.Runtime.InteropServices
-{
- [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
- public sealed class TypeIdentifierAttribute : Attribute
- {
- public TypeIdentifierAttribute() { }
- public TypeIdentifierAttribute(string scope, string identifier) { Scope_ = scope; Identifier_ = identifier; }
-
- public string Scope { get { return Scope_; } }
- public string Identifier { get { return Identifier_; } }
-
- internal string Scope_;
- internal string Identifier_;
- }
-
- // To be used on methods that sink reverse P/Invoke calls.
- // This attribute is a CoreCLR-only security measure, currently ignored by the desktop CLR.
- [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
- public sealed class AllowReversePInvokeCallsAttribute : Attribute
- {
- public AllowReversePInvokeCallsAttribute()
- {
- }
- }
-
- [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, Inherited = false)]
- public sealed class DispIdAttribute : Attribute
- {
- internal int _val;
-
- public DispIdAttribute(int dispId)
- {
- _val = dispId;
- }
-
- public int Value => _val;
- }
-
- [AttributeUsage(AttributeTargets.Class, Inherited = false)]
- public sealed class ComDefaultInterfaceAttribute : Attribute
- {
- internal Type _val;
-
- public ComDefaultInterfaceAttribute(Type defaultInterface)
- {
- _val = defaultInterface;
- }
-
- public Type Value => _val;
- }
-
- public enum ClassInterfaceType
- {
- None = 0,
- AutoDispatch = 1,
- AutoDual = 2
- }
-
- [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, Inherited = false)]
- public sealed class ClassInterfaceAttribute : Attribute
- {
- internal ClassInterfaceType _val;
-
- public ClassInterfaceAttribute(ClassInterfaceType classInterfaceType)
- {
- _val = classInterfaceType;
- }
-
- public ClassInterfaceAttribute(short classInterfaceType)
- {
- _val = (ClassInterfaceType)classInterfaceType;
- }
-
- public ClassInterfaceType Value => _val;
- }
-
- [AttributeUsage(AttributeTargets.Class, Inherited = false)]
- public sealed class ProgIdAttribute : Attribute
- {
- internal string _val;
-
- public ProgIdAttribute(string progId)
- {
- _val = progId;
- }
-
- public string Value => _val;
- }
-
- [AttributeUsage(AttributeTargets.Class, Inherited = true)]
- public sealed class ComSourceInterfacesAttribute : Attribute
- {
- internal string _val;
-
- public ComSourceInterfacesAttribute(string sourceInterfaces)
- {
- _val = sourceInterfaces;
- }
-
- public ComSourceInterfacesAttribute(Type sourceInterface)
- {
- _val = sourceInterface.FullName;
- }
-
- public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2)
- {
- _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName;
- }
-
- public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3)
- {
- _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName;
- }
-
- public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3, Type sourceInterface4)
- {
- _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName + "\0" + sourceInterface4.FullName;
- }
-
- public string Value => _val;
- }
-
- [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
- public sealed class CoClassAttribute : Attribute
- {
- internal Type _CoClass;
-
- public CoClassAttribute(Type coClass)
- {
- _CoClass = coClass;
- }
-
- public Type CoClass => _CoClass;
- }
-}
// means that the problem is already quite complex and we should not be dealing with it - see
// ComEventsMethod.Invoke
-using System;
-
+#nullable enable
namespace System.Runtime.InteropServices
{
/// <summary>
{
ComEventsInfo eventsInfo = ComEventsInfo.FromObject(rcw);
- ComEventsSink sink = eventsInfo.FindSink(ref iid);
+ ComEventsSink? sink = eventsInfo.FindSink(ref iid);
if (sink == null)
{
sink = eventsInfo.AddSink(ref iid);
}
- ComEventsMethod method = sink.FindMethod(dispid);
+ ComEventsMethod? method = sink.FindMethod(dispid);
if (method == null)
{
method = sink.AddMethod(dispid);
/// <param name="iid">identifier of the source interface used by COM object to fire events</param>
/// <param name="dispid">dispatch identifier of the method on the source interface</param>
/// <param name="d">delegate to remove from the invocation list</param>
- public static Delegate Remove(object rcw, Guid iid, int dispid, Delegate d)
+ public static Delegate? Remove(object rcw, Guid iid, int dispid, Delegate d)
{
lock (rcw)
{
- ComEventsInfo eventsInfo = ComEventsInfo.Find(rcw);
+ ComEventsInfo? eventsInfo = ComEventsInfo.Find(rcw);
if (eventsInfo == null)
{
return null;
}
- ComEventsSink sink = eventsInfo.FindSink(ref iid);
+ ComEventsSink? sink = eventsInfo.FindSink(ref iid);
if (sink == null)
{
return null;
}
- ComEventsMethod method = sink.FindMethod(dispid);
+ ComEventsMethod? method = sink.FindMethod(dispid);
if (method == null)
{
return null;
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using ComTypes = System.Runtime.InteropServices.ComTypes;
-
+#nullable enable
namespace System.Runtime.InteropServices
{
internal class ComEventsInfo
{
- private ComEventsSink _sinks;
+ private ComEventsSink? _sinks;
private object _rcw;
private ComEventsInfo(object rcw)
_sinks = ComEventsSink.RemoveAll(_sinks);
}
- public static ComEventsInfo Find(object rcw)
+ public static ComEventsInfo? Find(object rcw)
{
- return (ComEventsInfo)Marshal.GetComObjectData(rcw, typeof(ComEventsInfo));
+ return (ComEventsInfo?)Marshal.GetComObjectData(rcw, typeof(ComEventsInfo));
}
// it is caller's responsibility to call this method under lock(rcw)
public static ComEventsInfo FromObject(object rcw)
{
- ComEventsInfo eventsInfo = Find(rcw);
+ ComEventsInfo? eventsInfo = Find(rcw);
if (eventsInfo == null)
{
eventsInfo = new ComEventsInfo(rcw);
return eventsInfo;
}
- public ComEventsSink FindSink(ref Guid iid)
+ public ComEventsSink? FindSink(ref Guid iid)
{
return ComEventsSink.Find(_sinks, ref iid);
}
// it is caller's responsibility to call this method under lock(rcw)
internal ComEventsSink RemoveSink(ComEventsSink sink)
{
- _sinks = ComEventsSink.Remove(_sinks, sink);
- return _sinks;
+ _sinks = ComEventsSink.Remove(_sinks!, sink);
+ return _sinks!;
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
+#nullable enable
using System.Collections.Generic;
using System.Diagnostics;
-using System.Text;
-using System.Runtime.InteropServices;
using System.Reflection;
namespace System.Runtime.InteropServices
{
private bool _once = false;
private int _expectedParamsCount;
- private Type[] _cachedTargetTypes;
+ private Type?[]? _cachedTargetTypes;
public DelegateWrapper(Delegate d)
{
public Delegate Delegate { get; set; }
- public object Invoke(object[] args)
+ public object? Invoke(object[] args)
{
if (Delegate == null)
{
{
if (_cachedTargetTypes[i] != null)
{
- args[i] = Enum.ToObject(_cachedTargetTypes[i], args[i]);
+ args[i] = Enum.ToObject(_cachedTargetTypes[i]!, args[i]); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/34644
}
}
}
bool needToHandleCoercion = false;
- var targetTypes = new List<Type>();
+ var targetTypes = new List<Type?>();
foreach (ParameterInfo pi in parameters)
{
- Type targetType = null;
+ Type? targetType = null;
// recognize only 'ref Enum' signatures and cache
// both enum type and the underlying type.
private List<DelegateWrapper> _delegateWrappers = new List<DelegateWrapper>();
private readonly int _dispid;
- private ComEventsMethod _next;
+ private ComEventsMethod? _next;
public ComEventsMethod(int dispid)
{
_dispid = dispid;
}
- public static ComEventsMethod Find(ComEventsMethod methods, int dispid)
+ public static ComEventsMethod? Find(ComEventsMethod? methods, int dispid)
{
while (methods != null && methods._dispid != dispid)
{
return methods;
}
- public static ComEventsMethod Add(ComEventsMethod methods, ComEventsMethod method)
+ public static ComEventsMethod Add(ComEventsMethod? methods, ComEventsMethod method)
{
method._next = methods;
return method;
}
- public static ComEventsMethod Remove(ComEventsMethod methods, ComEventsMethod method)
+ public static ComEventsMethod? Remove(ComEventsMethod methods, ComEventsMethod method)
{
Debug.Assert(methods != null, "removing method from empty methods collection");
Debug.Assert(method != null, "specify method is null");
if (methods == method)
{
- methods = methods._next;
+ return methods._next;
}
else
{
- ComEventsMethod current = methods;
+ ComEventsMethod? current = methods;
+
while (current != null && current._next != method)
{
current = current._next;
{
current._next = method._next;
}
- }
- return methods;
+ return methods;
+ }
}
public bool Empty
{
if (wrapper.Delegate.GetType() == d.GetType())
{
- wrapper.Delegate = Delegate.Combine(wrapper.Delegate, d);
+ wrapper.Delegate = Delegate.Combine(wrapper.Delegate, d)!; // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
return;
}
}
{
// Find delegate wrapper index
int removeIdx = -1;
- DelegateWrapper wrapper = null;
+ DelegateWrapper? wrapper = null;
for (int i = 0; i < _delegateWrappers.Count; i++)
{
DelegateWrapper wrapperMaybe = _delegateWrappers[i];
}
// Update wrapper or remove from collection
- Delegate newDelegate = Delegate.Remove(wrapper.Delegate, d);
+ Delegate? newDelegate = Delegate.Remove(wrapper!.Delegate, d);
if (newDelegate != null)
{
wrapper.Delegate = newDelegate;
}
}
- public object Invoke(object[] args)
+ public object? Invoke(object[] args)
{
Debug.Assert(!Empty);
- object result = null;
+ object? result = null;
lock (_delegateWrappers)
{
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
+#nullable enable
using System.Diagnostics;
-using Variant = System.Runtime.InteropServices.Variant;
-
namespace System.Runtime.InteropServices
{
/// <summary>
internal class ComEventsSink : IDispatch, ICustomQueryInterface
{
private Guid _iidSourceItf;
- private ComTypes.IConnectionPoint _connectionPoint;
+ private ComTypes.IConnectionPoint? _connectionPoint;
private int _cookie;
- private ComEventsMethod _methods;
- private ComEventsSink _next;
+ private ComEventsMethod? _methods;
+ private ComEventsSink? _next;
public ComEventsSink(object rcw, Guid iid)
{
this.Advise(rcw);
}
- public static ComEventsSink Find(ComEventsSink sinks, ref Guid iid)
+ public static ComEventsSink? Find(ComEventsSink? sinks, ref Guid iid)
{
- ComEventsSink sink = sinks;
+ ComEventsSink? sink = sinks;
while (sink != null && sink._iidSourceItf != iid)
{
sink = sink._next;
return sink;
}
- public static ComEventsSink Add(ComEventsSink sinks, ComEventsSink sink)
+ public static ComEventsSink Add(ComEventsSink? sinks, ComEventsSink sink)
{
sink._next = sinks;
return sink;
}
- public static ComEventsSink RemoveAll(ComEventsSink sinks)
+ public static ComEventsSink? RemoveAll(ComEventsSink? sinks)
{
while (sinks != null)
{
return null;
}
- public static ComEventsSink Remove(ComEventsSink sinks, ComEventsSink sink)
+ public static ComEventsSink? Remove(ComEventsSink sinks, ComEventsSink sink)
{
Debug.Assert(sinks != null, "removing event sink from empty sinks collection");
Debug.Assert(sink != null, "specify event sink is null");
+ ComEventsSink? toReturn = sinks;
+
if (sink == sinks)
{
- sinks = sinks._next;
+ toReturn = sinks._next;
}
else
{
- ComEventsSink current = sinks;
+ ComEventsSink? current = sinks;
while (current != null && current._next != sink)
{
current = current._next;
sink.Unadvise();
- return sinks;
+ return toReturn;
}
- public ComEventsMethod RemoveMethod(ComEventsMethod method)
+ public ComEventsMethod? RemoveMethod(ComEventsMethod method)
{
- _methods = ComEventsMethod.Remove(_methods, method);
+ _methods = ComEventsMethod.Remove(_methods!, method);
return _methods;
}
- public ComEventsMethod FindMethod(int dispid)
+ public ComEventsMethod? FindMethod(int dispid)
{
return ComEventsMethod.Find(_methods, dispid);
}
IntPtr pExcepInfo,
IntPtr puArgErr)
{
- ComEventsMethod method = FindMethod(dispid);
+ ComEventsMethod? method = FindMethod(dispid);
if (method == null)
{
return;
// arguments marshalling. see code:ComEventsHelper#ComEventsArgsMarshalling
const int InvalidIdx = -1;
- object [] args = new object[pDispParams.cArgs];
+ object[] args = new object[pDispParams.cArgs];
int [] byrefsMap = new int[pDispParams.cArgs];
bool [] usedArgs = new bool[pDispParams.cArgs];
{
pos = namedArgs[i];
ref Variant pvar = ref GetVariant(ref vars[i]);
- args[pos] = pvar.ToObject();
+ args[pos] = pvar.ToObject()!;
usedArgs[pos] = true;
int byrefIdx = InvalidIdx;
}
ref Variant pvar = ref GetVariant(ref vars[pDispParams.cArgs - 1 - i]);
- args[pos] = pvar.ToObject();
+ args[pos] = pvar.ToObject()!;
int byrefIdx = InvalidIdx;
if (pvar.IsByRef)
}
// Do the actual delegate invocation
- object result = method.Invoke(args);
+ object? result = method.Invoke(args);
// convert result to VARIANT
if (pVarResult != IntPtr.Zero)
ComTypes.IConnectionPointContainer cpc = (ComTypes.IConnectionPointContainer)rcw;
ComTypes.IConnectionPoint cp;
- cpc.FindConnectionPoint(ref _iidSourceItf, out cp);
+ cpc.FindConnectionPoint(ref _iidSourceItf, out cp!);
object sinkObject = this;
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Runtime.CompilerServices;
#if BIT64
using nint = System.Int64;
public partial struct GCHandle
{
[MethodImpl(MethodImplOptions.InternalCall)]
- private static extern IntPtr InternalAlloc(object value, GCHandleType type);
+ private static extern IntPtr InternalAlloc(object? value, GCHandleType type);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void InternalFree(IntPtr handle);
#endif
[MethodImpl(MethodImplOptions.InternalCall)]
- private static extern void InternalSet(IntPtr handle, object value);
+ private static extern void InternalSet(IntPtr handle, object? value);
[MethodImpl(MethodImplOptions.InternalCall)]
- internal static extern object InternalCompareExchange(IntPtr handle, object value, object oldValue);
+ internal static extern object? InternalCompareExchange(IntPtr handle, object? value, object? oldValue);
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System;
using System.Collections.Generic;
using System.Text;
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
/// Therefore, this table uses weak assembly pointers to indirectly achieve
/// similar behavior.
/// </summary>
- private static ConditionalWeakTable<Assembly, DllImportResolver> s_nativeDllResolveMap = null;
+ private static ConditionalWeakTable<Assembly, DllImportResolver>? s_nativeDllResolveMap;
/// <summary>
/// Set a callback for resolving native library imports from an assembly.
try
{
- s_nativeDllResolveMap.Add(assembly, resolver);
+ s_nativeDllResolveMap!.Add(assembly, resolver); // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/26761
}
catch (ArgumentException)
{
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+#nullable enable
using System.Diagnostics;
namespace System.Runtime.InteropServices
/// Get the managed object representing the Variant.
/// </summary>
/// <returns></returns>
- public object ToObject()
+ public object? ToObject()
{
// Check the simple case upfront
if (IsEmpty)
// VT_UNKNOWN
- public object AsUnknown
+ public object? AsUnknown
{
get
{
// VT_DISPATCH
- public object AsDispatch
+ public object? AsDispatch
{
get
{
// implementation.
private static Assembly? Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
{
- AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target);
+ AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target)!;
return context.ResolveUsingLoad(assemblyName);
}
// after trying assembly resolution via Load override and TPA load context without success.
private static Assembly ResolveUsingResolvingEvent(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)
{
- AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target);
+ AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target)!;
// Invoke the AssemblyResolve event callbacks if wired up
return context.ResolveUsingEvent(assemblyName);
// implementation.
private static IntPtr ResolveUnmanagedDll(string unmanagedDllName, IntPtr gchManagedAssemblyLoadContext)
{
- AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target);
+ AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target)!;
return context.LoadUnmanagedDll(unmanagedDllName);
}
// after trying all other means of resolution.
private static IntPtr ResolveUnmanagedDllUsingEvent(string unmanagedDllName, Assembly assembly, IntPtr gchManagedAssemblyLoadContext)
{
- AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target);
+ AssemblyLoadContext context = (AssemblyLoadContext)(GCHandle.FromIntPtr(gchManagedAssemblyLoadContext).Target)!;
return context.GetResolvedUnmanagedDll(assembly, unmanagedDllName);
}
}
else
{
- loadContextForAssembly = (AssemblyLoadContext)(GCHandle.FromIntPtr(ptrAssemblyLoadContext).Target);
+ loadContextForAssembly = (AssemblyLoadContext)(GCHandle.FromIntPtr(ptrAssemblyLoadContext).Target)!;
}
}