本文整理汇总了C#中IComponent.CreateProperty方法的典型用法代码示例。如果您正苦于以下问题:C# IComponent.CreateProperty方法的具体用法?C# IComponent.CreateProperty怎么用?C# IComponent.CreateProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IComponent
的用法示例。
在下文中一共展示了IComponent.CreateProperty方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Cast
public object Cast(IComponent component, Type targetType)
{
Guard.NotNull(() => component, component);
Guard.NotNull(() => targetType, targetType);
if (!targetType.IsInterface)
throw new ArgumentException(Strings.SmartCast.InterfaceRequired);
if (component.Schema == null)
throw new ArgumentException(Strings.SmartCast.SchemaRequired(component));
var propertyName = "$" + targetType.AssemblyQualifiedName;
// NOTE: we cache the proxy instance for performance.
var proxyProperty = component.Properties.FirstOrDefault(p => p.Name == propertyName);
if (proxyProperty == null)
{
var containerSchema = component.Schema as IContainerInfo;
if (containerSchema != null)
{
if (!IsCompatibleContainer(containerSchema, targetType))
return null;
}
else if (!IsCompatibleComponent(component.Schema, targetType))
{
return null;
}
proxyProperty = component.CreateProperty(propertyName);
// Try to expose the most specific interface of the received component.
var componentType = component is IProduct ? typeof(IProduct) :
(component is IElement ? typeof(IElement) :
(component is ICollection ? typeof(ICollection) : typeof(IComponent)));
proxyProperty.Value = generator.CreateInterfaceProxyWithTarget(
componentType, new[] { targetType }, component, new Interceptor(component, Behaviors.Default));
}
return proxyProperty.Value;
}