[Core] Assignable types API
This commit is contained in:
Родитель
53b9a841c1
Коммит
5f87e208b9
|
@ -20,6 +20,11 @@ namespace Xamarin.PropertyEditing.Tests
|
|||
return Task.FromResult (editor);
|
||||
}
|
||||
|
||||
public Task<object> CreateObjectAsync (ITypeInfo type)
|
||||
{
|
||||
throw new System.NotImplementedException ();
|
||||
}
|
||||
|
||||
private Dictionary<object, IObjectEditor> editorCache = new Dictionary<object, IObjectEditor> ();
|
||||
}
|
||||
}
|
|
@ -106,6 +106,11 @@ namespace Xamarin.PropertyEditing.Tests
|
|||
return Task.FromResult<IReadOnlyList<string>> (new string[0]);
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<ITypeInfo>> GetAssignableTypesAsync (IPropertyInfo property)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
public async Task SetValueAsync<T> (IPropertyInfo property, ValueInfo<T> value, PropertyVariation variation = null)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
using System;
|
||||
|
||||
namespace Xamarin.PropertyEditing
|
||||
{
|
||||
public interface IAssemblyInfo
|
||||
{
|
||||
string Name { get; }
|
||||
}
|
||||
|
||||
public class AssemblyInfo
|
||||
: IAssemblyInfo, IEquatable<AssemblyInfo>
|
||||
{
|
||||
public AssemblyInfo (string name)
|
||||
{
|
||||
if (name == null)
|
||||
throw new ArgumentNullException (nameof(name));
|
||||
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public bool Equals (AssemblyInfo other)
|
||||
{
|
||||
if (ReferenceEquals (null, other))
|
||||
return false;
|
||||
if (ReferenceEquals (this, other))
|
||||
return true;
|
||||
|
||||
return string.Equals (Name, other.Name);
|
||||
}
|
||||
|
||||
public override bool Equals (object obj)
|
||||
{
|
||||
if (ReferenceEquals (null, obj))
|
||||
return false;
|
||||
if (ReferenceEquals (this, obj))
|
||||
return true;
|
||||
if (obj.GetType () != GetType ())
|
||||
return false;
|
||||
|
||||
return Equals ((AssemblyInfo) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode ()
|
||||
{
|
||||
return Name.GetHashCode ();
|
||||
}
|
||||
|
||||
public static bool operator == (AssemblyInfo left, AssemblyInfo right)
|
||||
{
|
||||
return Equals (left, right);
|
||||
}
|
||||
|
||||
public static bool operator != (AssemblyInfo left, AssemblyInfo right)
|
||||
{
|
||||
return !Equals (left, right);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,14 @@
|
|||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Xamarin.PropertyEditing
|
||||
{
|
||||
public interface IEditorProvider
|
||||
{
|
||||
Task<IObjectEditor> GetObjectEditorAsync (object item);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a representation value of the <paramref name="type"/> and returns it.
|
||||
/// </summary>
|
||||
Task<object> CreateObjectAsync (ITypeInfo type);
|
||||
}
|
||||
}
|
|
@ -58,6 +58,8 @@ namespace Xamarin.PropertyEditing
|
|||
/// </summary>
|
||||
event EventHandler<EditorPropertyChangedEventArgs> PropertyChanged;
|
||||
|
||||
Task<IReadOnlyList<ITypeInfo>> GetAssignableTypesAsync (IPropertyInfo property);
|
||||
|
||||
/*
|
||||
* Dealing with async values in the context of what's possible across all target platforms is a bit complex.
|
||||
* While implicit safety may be able to be ensured, it would be exhaustive to reason it out and could change
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
using System;
|
||||
|
||||
namespace Xamarin.PropertyEditing
|
||||
{
|
||||
public interface ITypeInfo
|
||||
{
|
||||
IAssemblyInfo Assembly { get; }
|
||||
string NameSpace { get; }
|
||||
string Name { get; }
|
||||
}
|
||||
|
||||
public class TypeInfo
|
||||
: ITypeInfo, IEquatable<TypeInfo>
|
||||
{
|
||||
public TypeInfo (IAssemblyInfo assembly, string nameSpace, string name)
|
||||
{
|
||||
if (assembly == null)
|
||||
throw new ArgumentNullException (nameof(assembly));
|
||||
if (nameSpace == null)
|
||||
throw new ArgumentNullException (nameof(nameSpace));
|
||||
if (name == null)
|
||||
throw new ArgumentNullException (nameof(name));
|
||||
|
||||
Assembly = assembly;
|
||||
NameSpace = nameSpace;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public IAssemblyInfo Assembly
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public string NameSpace
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public bool Equals (TypeInfo other)
|
||||
{
|
||||
if (ReferenceEquals (null, other))
|
||||
return false;
|
||||
if (ReferenceEquals (this, other))
|
||||
return true;
|
||||
|
||||
return Assembly.Equals (other.Assembly) && String.Equals (NameSpace, other.NameSpace) && String.Equals (Name, other.Name);
|
||||
}
|
||||
|
||||
public override bool Equals (object obj)
|
||||
{
|
||||
if (ReferenceEquals (null, obj))
|
||||
return false;
|
||||
if (ReferenceEquals (this, obj))
|
||||
return true;
|
||||
if (obj.GetType () != GetType ())
|
||||
return false;
|
||||
|
||||
return Equals ((TypeInfo) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode ()
|
||||
{
|
||||
unchecked {
|
||||
var hashCode = Assembly.GetHashCode ();
|
||||
hashCode = (hashCode * 397) ^ NameSpace.GetHashCode ();
|
||||
hashCode = (hashCode * 397) ^ Name.GetHashCode ();
|
||||
return hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool operator == (TypeInfo left, TypeInfo right)
|
||||
{
|
||||
return Equals (left, right);
|
||||
}
|
||||
|
||||
public static bool operator != (TypeInfo left, TypeInfo right)
|
||||
{
|
||||
return !Equals (left, right);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -10,5 +10,20 @@ namespace Xamarin.PropertyEditing.Reflection
|
|||
{
|
||||
return Task.FromResult<IObjectEditor> (new ReflectionObjectEditor (item));
|
||||
}
|
||||
|
||||
public Task<object> CreateObjectAsync (ITypeInfo type)
|
||||
{
|
||||
var realType = GetRealType (type);
|
||||
if (realType == null)
|
||||
return Task.FromResult<object> (null);
|
||||
|
||||
object instance = Activator.CreateInstance (realType);
|
||||
return Task.FromResult (instance);
|
||||
}
|
||||
|
||||
public static Type GetRealType (ITypeInfo type)
|
||||
{
|
||||
return Type.GetType ($"{type.NameSpace}.{type.Name}, {type.Assembly.Name}");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -92,6 +92,21 @@ namespace Xamarin.PropertyEditing.Reflection
|
|||
return Task.FromResult (info.GetHandlers (this.target));
|
||||
}
|
||||
|
||||
public Task<IReadOnlyList<ITypeInfo>> GetAssignableTypesAsync (IPropertyInfo property)
|
||||
{
|
||||
return Task.Run (() => {
|
||||
ReflectionPropertyInfo realInfo = (ReflectionPropertyInfo) property;
|
||||
var assemblies = AppDomain.CurrentDomain.GetAssemblies ();
|
||||
|
||||
return (IReadOnlyList<ITypeInfo>)assemblies.Select (a => new { Assembly = a, Info = new AssemblyInfo (a.FullName)})
|
||||
.SelectMany (a =>
|
||||
a.Assembly.DefinedTypes.Select (t => new { Type = t, Assembly = a }))
|
||||
.AsParallel ()
|
||||
.Where (t => realInfo.Type.IsAssignableFrom (t.Type))
|
||||
.Select (t => new TypeInfo (t.Assembly.Info, t.Type.Namespace, t.Type.Name)).ToList();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task SetValueAsync<T> (IPropertyInfo property, ValueInfo<T> value, PropertyVariation variation = null)
|
||||
{
|
||||
if (property == null)
|
||||
|
|
|
@ -63,6 +63,7 @@
|
|||
<Compile Include="Drawing\CommonTileBrush.cs" />
|
||||
<Compile Include="Drawing\CommonTileMode.cs" />
|
||||
<Compile Include="Drawing\CommonImageBrush.cs" />
|
||||
<Compile Include="IAssemblyInfo.cs" />
|
||||
<Compile Include="IAvailabilityConstraint.cs" />
|
||||
<Compile Include="IClampedPropertyInfo.cs" />
|
||||
<Compile Include="IColorSpaced.cs" />
|
||||
|
@ -75,6 +76,7 @@
|
|||
<Compile Include="IPropertyInfo.cs" />
|
||||
<Compile Include="IResourceProvider.cs" />
|
||||
<Compile Include="ISelfConstrainedPropertyInfo.cs" />
|
||||
<Compile Include="ITypeInfo.cs" />
|
||||
<Compile Include="MultiAvailabilityConstraint.cs" />
|
||||
<Compile Include="NotifyingObject.cs" />
|
||||
<Compile Include="ObservableCollectionEx.cs" />
|
||||
|
|
Загрузка…
Ссылка в новой задаче