Skip to content

Commit

Permalink
fix componentService
Browse files Browse the repository at this point in the history
  • Loading branch information
CoJaques committed Aug 18, 2024
1 parent 4245300 commit 343aea5
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright © 2024 Lionk Project

using System.Collections.Concurrent;
using System.Reflection;
using Lionk.Core.TypeRegistery;

namespace Lionk.Core.Component;
Expand All @@ -23,7 +24,10 @@ public ComponentService(ITypesProvider provider)
/// <inheritdoc/>
public void RegisterComponentInstance(IComponent component)
{
string baseName = component.InstanceName ?? DefaultComponentName;
if (component.GetType().GetCustomAttribute<NamedElement>() is NamedElement attribute)
component.InstanceName = attribute.Name;

string baseName = component.InstanceName == string.Empty ? DefaultComponentName : component.InstanceName;
string uniqueName = GenerateUniqueName(baseName);
component.InstanceName = uniqueName;
_componentInstances.TryAdd(uniqueName, component);
Expand All @@ -48,7 +52,7 @@ public IReadOnlyDictionary<ComponentTypeDescription, Factory> GetRegisteredTypeD
/// <inheritdoc/>
public IComponent? GetInstanceByName(string name)
{
_componentInstances.TryGetValue(name, out IComponent? component);
IComponent? component = _componentInstances.Values.Where(x => x.InstanceName == name).FirstOrDefault();
return component;
}

Expand Down

0 comments on commit 343aea5

Please sign in to comment.