本文整理汇总了C#中Zenject.InjectContext类的典型用法代码示例。如果您正苦于以下问题:C# InjectContext类的具体用法?C# InjectContext怎么用?C# InjectContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
InjectContext类属于Zenject命名空间,在下文中一共展示了InjectContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(
InjectContext context, List<TypeValuePair> args)
{
Assert.IsNotNull(context);
var gameObjectRunner = _prefabInstantiator.Instantiate(args);
// First get instance
bool hasMore = gameObjectRunner.MoveNext();
var gameObject = gameObjectRunner.Current;
var allComponents = gameObject.GetComponentsInChildren(_componentType);
Assert.That(allComponents.Length >= 1,
"Expected to find at least one component with type '{0}' on prefab '{1}'",
_componentType, _prefabInstantiator.GetPrefab().name);
yield return allComponents.Cast<object>().ToList();
// Now do injection
while (hasMore)
{
hasMore = gameObjectRunner.MoveNext();
}
}
示例2: CreateInjectContext
public InjectContext CreateInjectContext(
DiContainer container, InjectContext currentContext, object targetInstance, string concreteIdentifier)
{
return new InjectContext(
container, MemberType, Identifier, Optional,
ObjectType, targetInstance, MemberName, currentContext, concreteIdentifier);
}
示例3: GetInstance
public override object GetInstance(InjectContext context)
{
Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));
return context.Container.InstantiateComponentOnNewGameObjectExplicit(
_componentType, _componentType.Name(), new List<TypeValuePair>(), context);
}
示例4: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
{
Assert.IsNotNull(context);
object instance;
// We still want to make sure we can get the game object during validation
var gameObj = GetGameObject(context);
if (!_container.IsValidating || DiContainer.CanCreateOrInjectDuringValidation(_componentType))
{
instance = gameObj.AddComponent(_componentType);
}
else
{
instance = new ValidationMarker(_componentType);
}
// Note that we don't just use InstantiateComponentOnNewGameObjectExplicit here
// because then circular references don't work
yield return new List<object>() { instance };
var injectArgs = new InjectArgs()
{
ExtraArgs = _extraArguments.Concat(args).ToList(),
UseAllArgs = true,
Context = context,
ConcreteIdentifier = _concreteIdentifier,
};
_container.InjectExplicit(instance, _componentType, injectArgs);
Assert.That(injectArgs.ExtraArgs.IsEmpty());
}
示例5: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
{
Assert.IsNotNull(context);
bool autoInject = false;
var instanceType = GetTypeToCreate(context.MemberType);
var injectArgs = new InjectArgs()
{
ExtraArgs = _extraArguments.Concat(args).ToList(),
Context = context,
ConcreteIdentifier = _concreteIdentifier,
UseAllArgs = false,
};
var instance = _container.InstantiateExplicit(
instanceType, autoInject, injectArgs);
// Return before property/field/method injection to allow circular dependencies
yield return new List<object>() { instance };
injectArgs.UseAllArgs = true;
_container.InjectExplicit(instance, instanceType, injectArgs);
}
示例6: GetComponent
public object GetComponent(Type componentType, InjectContext context)
{
if (_rootObj == null)
{
Assert.IsNotNull(_id.ResourcePath);
var prefab = (GameObject)Resources.Load(_id.ResourcePath);
Assert.IsNotNull(prefab, "Could not find prefab at resource path '{0}'", _id.ResourcePath);
_rootObj = (GameObject)GameObject.Instantiate(prefab);
// Note that we always want to cache _container instead of using context.Container
// since for singletons, the container they are accessed from should not determine
// the container they are instantiated with
// Transients can do that but not singletons
_rootObj.transform.SetParent(_container.DefaultParent, false);
_rootObj.SetActive(true);
_container.InjectGameObject(_rootObj, true, false, new object[0], context);
}
var component = _rootObj.GetComponentInChildren(componentType);
if (component == null)
{
throw new ZenjectResolveException(
"Could not find component with type '{0}' in given singleton prefab".Fmt(componentType));
}
return component;
}
示例7: ValidateBinding
public IEnumerable<ZenjectResolveException> ValidateBinding(
Type componentType, InjectContext context)
{
if (!ContainsComponent(componentType))
{
yield return new ZenjectResolveException(
"Could not find component of type '{0}' in prefab with name '{1}' \nObject graph:\n{2}"
.Fmt(componentType.Name(), _id.Prefab.name, context.GetObjectGraphString()));
yield break;
}
// In most cases componentType will be a MonoBehaviour but we also want to allow interfaces
// And in that case we can't validate it
if (!componentType.IsAbstract())
{
// Note that we always want to cache _container instead of using context.Container
// since for singletons, the container they are accessed from should not determine
// the container they are instantiated with
// Transients can do that but not singletons
foreach (var err in _container.ValidateObjectGraph(componentType, context))
{
yield return err;
}
}
}
示例8: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
{
Assert.IsNotNull(context);
if (_instances != null)
{
yield return _instances;
yield break;
}
// This should only happen with constructor injection
// Field or property injection should allow circular dependencies
Assert.That(!_isCreatingInstance,
"Found circular dependency when creating type '{0}'",
_creator.GetInstanceType(context));
_isCreatingInstance = true;
var runner = _creator.GetAllInstancesWithInjectSplit(context, args);
// First get instance
bool hasMore = runner.MoveNext();
_instances = runner.Current;
Assert.IsNotNull(_instances);
_isCreatingInstance = false;
yield return _instances;
// Now do injection
while (hasMore)
{
hasMore = runner.MoveNext();
}
}
示例9: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
{
Assert.IsEmpty(args);
Assert.IsNotNull(context);
if (_container.IsValidating)
{
yield return new List<object>() { new ValidationMarker(context.MemberType) };
}
else
{
var result = _method(context);
if (result == null)
{
Assert.That(context.MemberType.IsPrimitive,
"Invalid value returned from FromMethod. Expected non-null.");
}
else
{
Assert.That(result.GetType().DerivesFromOrEqual(context.MemberType));
}
yield return new List<object>() { result };
}
}
示例10: GetInstance
public override object GetInstance(InjectContext context)
{
Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));
if (_instance == null)
{
// This is valid sometimes
//Assert.That(!_container.IsValidating,
//"Tried to instantiate a MonoBehaviour with type '{0}' during validation. Object graph: {1}", _componentType, DiContainer.GetCurrentObjectGraph());
// Note that we always want to cache _container instead of using context.Container
// since for singletons, the container they are accessed from should not determine
// the container they are instantiated with
// Transients can do that but not singletons
var name = string.IsNullOrEmpty(_name) ? _componentType.Name() : _name;
// We don't use the generic version here to avoid duplicate generic arguments to binder
_instance = _container.InstantiateComponentOnNewGameObjectExplicit(
_componentType, name, new List<TypeValuePair>(), context);
Assert.IsNotNull(_instance);
}
return _instance;
}
示例11: GetInstance
public override object GetInstance(InjectContext context)
{
var obj = context.Container.InstantiateExplicit(
GetTypeToInstantiate(context.MemberType), new List<TypeValuePair>(), context, null, true);
Assert.That(obj != null);
return obj;
}
示例12: GetSubContext
InjectContext GetSubContext(InjectContext parent)
{
var subContext = parent.CreateSubContext(_contractType, _identifier);
subContext.Optional = _isOptional;
return subContext;
}
示例13: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
{
Assert.IsEmpty(args);
Assert.IsNotNull(context);
Assert.That(_instanceType.DerivesFromOrEqual(context.MemberType));
yield return new List<object>() { _instance };
}
示例14: GetInstance
public override object GetInstance(InjectContext context)
{
Assert.That(_componentType.DerivesFromOrEqual(context.MemberType));
var name = string.IsNullOrEmpty(_name) ? _componentType.Name() : _name;
return context.Container.InstantiateComponentOnNewGameObjectExplicit(
_componentType, name, new List<TypeValuePair>(), context);
}
示例15: GetAllInstancesWithInjectSplit
public IEnumerator<List<object>> GetAllInstancesWithInjectSplit(InjectContext context, List<TypeValuePair> args)
{
Assert.IsEmpty(args);
Assert.IsNotNull(context);
Assert.That(_contractType.DerivesFromOrEqual(context.MemberType));
yield return _container.ResolveAll(GetSubContext(context)).Cast<object>().ToList();
}