Beispiel #1
0
        public DeputyBase GetOrCreateNativeReference(ByRefValue byRefValue)
        {
            if (!_references.ContainsKey(byRefValue.Value))
            {
                ConstructorInfo constructorInfo = GetByRefConstructor();
                _references[byRefValue.Value] = (DeputyBase)constructorInfo.Invoke(new object[] { byRefValue });
            }

            return(_references[byRefValue.Value]);

            ConstructorInfo GetByRefConstructor()
            {
                Type type = _types.GetClassType(byRefValue.FullyQualifiedName);

                // If type is an interface or abstract class
                if (type == null || type.IsAbstract)
                {
                    type = _types.GetProxyType(byRefValue.FullyQualifiedName);
                }

                if (type == null)
                {
                    throw new ArgumentException(
                              $"Could not find type with fully qualified name '{byRefValue.FullyQualifiedName}'",
                              nameof(byRefValue)
                              );
                }

                BindingFlags constructorFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

                return(type.GetConstructor(constructorFlags, null, new[] { typeof(ByRefValue) }, null));
            }
        }
            protected TestBase()
            {
                _typeCache    = Substitute.For <ITypeCache>();
                _referenceMap = Substitute.For <IReferenceMap>();
                _converter    = new FrameworkToJsiiConverter(_typeCache);

                IServiceCollection serviceCollection = new ServiceCollection();

                serviceCollection.AddSingleton <ITypeCache>(_typeCache);
                serviceCollection.AddSingleton <IReferenceMap>(_referenceMap);
                serviceCollection.AddSingleton <IFrameworkToJsiiConverter>(_converter);
                serviceCollection.AddSingleton <IClient>(Substitute.For <IClient>());
                serviceCollection.AddTransient <IClient>(sp => Substitute.For <IClient>());

                IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

                ServiceContainer.ServiceProviderOverride = serviceProvider;

                _typeCache.GetClassType("myClassFqn").Returns(typeof(TestClass));
                _typeCache.GetEnumType("myEnumFqn").Returns(typeof(TestEnum));
            }
Beispiel #3
0
        bool TryConvertCustomType(IReferenceMap referenceMap, object value, bool isOptional, string fullyQualifiedName, out object result)
        {
            if (IsReferenceType())
            {
                return(TryConvertClass(referenceMap, value, out result));
            }

            if (_types.GetEnumType(fullyQualifiedName) != null)
            {
                return(TryConvertEnum(value, isOptional, fullyQualifiedName, out result));
            }

            result = null;
            return(false);

            bool IsReferenceType()
            {
                return
                    (_types.GetClassType(fullyQualifiedName) != null ||
                     _types.GetInterfaceType(fullyQualifiedName) != null ||
                     _types.GetProxyType(fullyQualifiedName) != null);
            }
        }