Reputation:
Here I am using GetType()
to get the type of eventData
as follows,
public EventDescriptor(Guid id, IEvent eventData)
{
AggregateId = id;
EventData = eventData;
EventType = eventData.GetType().AssemblyQualifiedName;
}
which returns:
"EventType":
"Shared.Events.EntityCreatedEvent`1[[Shared.Model.Dto.EntityDto, Shared,
Version=1.0.6928.25697, Culture=neutral, PublicKeyToken=null]], Shared,
Version=1.0.6928.25697, Culture=neutral, PublicKeyToken=null"
How can I get only the Shared.Model.Dto.EntityDto
from the above?
Is there any method or propertiy available?
Upvotes: 1
Views: 109
Reputation: 622
Try Type.FullName
to get what you need -
https://learn.microsoft.com/de-de/dotnet/api/system.type.fullname?view=netframework-4.7.2
So in your case -
public EventDescriptor(Guid id, IEvent eventData)
{
AggregateId = id;
EventData = eventData;
EventType = eventData.GetType().FullName;
}
If you want to extract the type of the generic argument of the IEvent instance, you can do this -
public EventDescriptor(Guid id, IEvent eventData)
{
AggregateId = id;
EventData = eventData;
if (!eventData.GetType().IsGenericType)
{
EventType = eventData.GetType().FullName;
}
else
{
// notice - this assumes we can take the FIRST generic argument, we don't check for others here
EventType = eventData.GetType().GetGenericArguments().First().FullName;
}
}
Upvotes: 2