Made some important constructors private. Removed some unneeded methods.

This commit is contained in:
Paul Knopf 2018-07-19 22:32:47 -04:00
parent 99adf58c9a
commit 140b62c470
2 changed files with 31 additions and 48 deletions

View file

@ -21,14 +21,5 @@ namespace Qt.NetCore.Tests.Types
o.Should().Be(returnedInstance);
}
[Fact]
public void Can_create_instance_from_type_info()
{
var typeInfo = NetTypeManager.GetTypeInfo<TestObject>();
var instance = NetInstance.InstantiateType(typeInfo);
instance.Should().NotBeNull();
instance.Instance.Should().BeOfType<TestObject>();
}
}
}

View file

@ -8,12 +8,7 @@ namespace Qt.NetCore.Types
{
public class NetInstance : BaseDisposable
{
static NetInstance()
{
}
public NetInstance(IntPtr gcHandle, NetTypeInfo type)
private NetInstance(IntPtr gcHandle, NetTypeInfo type)
:base(Interop.NetInstance.Create(gcHandle, type.Handle))
{
}
@ -24,39 +19,6 @@ namespace Qt.NetCore.Types
}
private static ConditionalWeakTable<object, NetInstance> _objectNetInstanceConnections = new ConditionalWeakTable<object, NetInstance>();
public static NetInstance GetForObject(object value)
{
if (value == null) return null;
bool alreadyExists = false;
if (_objectNetInstanceConnections.TryGetValue(value, out var netInstance))
{
alreadyExists = true;
if (GCHandle.FromIntPtr(netInstance.Handle).IsAllocated)
{
return netInstance;
}
}
var typeInfo = NetTypeManager.GetTypeInfo(GetUnproxiedType(value.GetType()).AssemblyQualifiedName);
if(typeInfo == null) throw new InvalidOperationException($"Couldn't create type info from {value.GetType().AssemblyQualifiedName}");
var handle = GCHandle.Alloc(value);
var newNetInstance = new NetInstance(GCHandle.ToIntPtr(handle), typeInfo);
if(alreadyExists)
{
_objectNetInstanceConnections.Remove(value);
}
_objectNetInstanceConnections.Add(value, newNetInstance);
return newNetInstance;
}
public static NetInstance InstantiateType(NetTypeInfo type)
{
var result = Interop.Callbacks.InstantiateType(type.Handle);
if (result == IntPtr.Zero) return null;
return new NetInstance(result);
}
public object Instance
{
get
@ -71,6 +33,8 @@ namespace Qt.NetCore.Types
Interop.NetInstance.Destroy(ptr);
}
#region Instance helpers
public static ITypeCreator TypeCreator { get; set; }
private static Type GetUnproxiedType(Type type)
@ -80,6 +44,34 @@ namespace Qt.NetCore.Types
return type;
}
private static readonly ConditionalWeakTable<object, NetInstance> ObjectNetInstanceConnections = new ConditionalWeakTable<object, NetInstance>();
public static NetInstance GetForObject(object value)
{
if (value == null) return null;
var alreadyExists = false;
if (ObjectNetInstanceConnections.TryGetValue(value, out var netInstance))
{
alreadyExists = true;
if (GCHandle.FromIntPtr(netInstance.Handle).IsAllocated)
{
return netInstance;
}
}
var typeInfo = NetTypeManager.GetTypeInfo(GetUnproxiedType(value.GetType()).AssemblyQualifiedName);
if(typeInfo == null) throw new InvalidOperationException($"Couldn't create type info from {value.GetType().AssemblyQualifiedName}");
var handle = GCHandle.Alloc(value);
var newNetInstance = new NetInstance(GCHandle.ToIntPtr(handle), typeInfo);
if(alreadyExists)
{
ObjectNetInstanceConnections.Remove(value);
}
ObjectNetInstanceConnections.Add(value, newNetInstance);
return newNetInstance;
}
#endregion
}
public interface INetInstanceInterop