Skip to content

Commit

Permalink
Merge pull request #375 from Cysharp/hotfix/GroupConfigurationAttribute
Browse files Browse the repository at this point in the history
Use ActivatorUtilities with the service provider instead of Activator
  • Loading branch information
mayuki authored Nov 18, 2020
2 parents c414891 + 5d718b0 commit 28553e4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/MagicOnion.Server/Hubs/Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ namespace MagicOnion.Server.Hubs
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class GroupConfigurationAttribute : Attribute
{
Type type;
public Type FactoryType { get; }

public GroupConfigurationAttribute(Type groupRepositoryFactoryType)
{
this.type = groupRepositoryFactoryType;
}
if (!typeof(IGroupRepositoryFactory).IsAssignableFrom(groupRepositoryFactoryType) && (groupRepositoryFactoryType.IsAbstract || groupRepositoryFactoryType.IsInterface))
{
throw new ArgumentException("A Group repository factory must implement IGroupRepositoryFactory interface and must be a concrete class.");
}

public IGroupRepositoryFactory Create()
{
return (IGroupRepositoryFactory)Activator.CreateInstance(type)!;
this.FactoryType = groupRepositoryFactoryType;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/MagicOnion.Server/MagicOnionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static MagicOnionServiceDefinition BuildServerServiceDefinition(IServiceP
var attr = classType.GetCustomAttribute<GroupConfigurationAttribute>(true);
if (attr != null)
{
factory = attr.Create();
factory = (IGroupRepositoryFactory)ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, attr.FactoryType);
}
else
{
Expand Down

0 comments on commit 28553e4

Please sign in to comment.