Answer the question
In order to leave comments, you need to log in
Why doesn't interface typecasting work?
Hello.
In a certain case, there is no conversion (cast) of an interface to a class.
To begin with, a typical working example:
There is an interface, there is an implementing class, there is another class with a static method that receives an interface as input and receives a class name through reflection. This method also performs a cast of the interface to the type and accesses the property defined in the type.
public interface ITest { }
public class CTest : ITest
{
public string Message { get; set; }
}
public class Testing
{
public static void DoIt(ITest t)
{
Console.WriteLine(t.GetType().Name);//->CTest
CTest ct = (CTest)t;
Console.WriteLine(ct.Message);//->test message
}
}
CTest t=new CTest(){ Message = "test message"};
Testing.DoIt(t);
Fault<out T>
FaultEvent<T>
public Task Consume(ConsumeContext<Fault> context)
{
Fault m = context.Message;
string typename = m.GetType().FullName;
}
Answer the question
In order to leave comments, you need to log in
https://github.com/MassTransit/MassTransit/blob/5e...
this is not the Fault you are thinking about. Somewhere in the bowels of GreenPipes there is another Fault class. So no magic happened -
using MassTransit;
using System;
namespace GreenPipes.DynamicInternal.MassTransit
{
public class MyFault<T> : Fault<T>
{
public T Message => throw new NotImplementedException();
public Guid FaultId => throw new NotImplementedException();
public Guid? FaultedMessageId => throw new NotImplementedException();
public DateTime Timestamp => throw new NotImplementedException();
public ExceptionInfo[] Exceptions => throw new NotImplementedException();
public HostInfo Host => throw new NotImplementedException();
public string[] FaultMessageTypes => throw new NotImplementedException();
}
public class Fault: MyFault<object>
{
}
public class Program
{
static void Main(string[] args)
{
var fault = new Fault();
string typename = fault.GetType().FullName; //GreenPipes.DynamicInternal.MassTransit.Fault
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question