internal static StatePersistenceAttribute Get(Type actorType) { var attribute = new StatePersistenceAttribute(StatePersistence.None); var attributes = actorType.GetTypeInfo().GetCustomAttributes(typeof(StatePersistenceAttribute), false); var enumerator = attributes.GetEnumerator(); if (enumerator.MoveNext()) { attribute.StatePersistence = ((StatePersistenceAttribute)enumerator.Current).StatePersistence; } return(attribute); }
/// <summary> /// Creates <see cref="ActorTypeInformation"/> from actorType. /// </summary> /// <param name="actorType">Type of class implementing the actor to create ActorTypeInforamtion for.</param> /// <returns><see cref="ActorTypeInformation"/> created from actorType.</returns> /// <exception cref="System.ArgumentException"> /// <para>When <see cref="System.Type.BaseType"/> for actorType is not of type <see cref="Actor"/>.</para> /// <para>When actorType does not implement an interface deriving from <see cref="IActor"/> /// and is not marked as abstract.</para> /// <para>When actorType implements more than one interface which derives from <see cref="IActor"/> /// but doesn't have <see cref="ActorServiceAttribute"/>.</para> /// </exception> public static ActorTypeInformation Get(Type actorType) { if (!actorType.IsActor()) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, SR.ErrorNotAnActor, actorType.FullName, typeof(Actor).FullName), "actorType"); } string actorServiceName = null; var actorServiceAttr = ActorServiceAttribute.Get(actorType); if (actorServiceAttr != null) { actorServiceName = actorServiceAttr.Name; } // get all actor interfaces var actorInterfaces = actorType.GetActorInterfaces(); // ensure that the if the actor type is not abstract it implements at least one actor interface if ((actorInterfaces.Length == 0) && (!actorType.GetTypeInfo().IsAbstract)) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, SR.ErrorNoActorInterfaceFound, actorType.FullName, typeof(IActor).FullName), "actorType"); } // ensure that all actor interfaces can be remoted foreach (var actorInterface in actorInterfaces) { ActorInterfaceDescription.Create(actorInterface); } // if the actor implements more than one actor interfaces make sure that it has actorServiceName if ((actorInterfaces.Length > 1) && string.IsNullOrEmpty(actorServiceName) && (!actorType.GetTypeInfo().IsAbstract)) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, SR.ErrorNoActorServiceNameMultipleInterfaces, actorType.FullName, typeof(ActorServiceAttribute).FullName), "actorType"); } // get actor event interfaces var eventInterfaces = actorType.GetActorEventInterfaces(); // ensure that all of the event interfaces can be remoted if (eventInterfaces != null) { foreach (var eventInterface in eventInterfaces) { ActorEventInterfaceDescription.Create(eventInterface); } } return(new ActorTypeInformation() { InterfaceTypes = actorInterfaces, ImplementationType = actorType, ServiceName = actorServiceName, IsAbstract = actorType.GetTypeInfo().IsAbstract, IsRemindable = actorType.IsRemindableActor(), EventInterfaceTypes = eventInterfaces, StatePersistence = StatePersistenceAttribute.Get(actorType).StatePersistence }); }