Click here to Skip to main content
15,884,661 members
Home / Discussions / C#
   

C#

 
GeneralRe: Forms opening other forms Pin
Matias Lopez11-Sep-18 8:50
Matias Lopez11-Sep-18 8:50 
QuestionC# project - SMS Alert Pin
Member 124675946-Sep-18 21:51
Member 124675946-Sep-18 21:51 
AnswerRe: C# project - SMS Alert Pin
OriginalGriff6-Sep-18 22:05
mveOriginalGriff6-Sep-18 22:05 
AnswerRe: C# project - SMS Alert Pin
DerekT-P13-Sep-18 1:01
professionalDerekT-P13-Sep-18 1:01 
QuestionCrypto in C#/4.0 using PKCS#10 and SHA256 Pin
ninodago6-Sep-18 5:28
ninodago6-Sep-18 5:28 
QuestionProblems with Rx EventMessager [Solved] Pin
Kenneth Haugland5-Sep-18 4:10
mvaKenneth Haugland5-Sep-18 4:10 
AnswerRe: Problems with Rx EventMessager Pin
Pete O'Hanlon5-Sep-18 4:49
mvePete O'Hanlon5-Sep-18 4:49 
AnswerRe: Problems with Rx EventMessager Pin
Richard Deeming5-Sep-18 4:54
mveRichard Deeming5-Sep-18 4:54 
Activator.CreateInstance(Type) returns an Object.
Activator.CreateInstance Method (System) | Microsoft Docs[^]

As a result, you are calling eventMessager.Publish<object>(instanceOfYourType).

That uses the run-time type of the parameter to find the subscriber. This will be a Subject<YourClass> instance.

You then try to cast that instance to Subject<object>, which fails because the Subject<T> class is not covariant on its type parameter.
Covariance and Contravariance in Generics | Microsoft Docs[^]

You can't even get away with casting the subscriber to the IObserver<T> interface, since that interface is contravariant.

You're going to need to use reflection to invoke the method. I'd suggest using a separate method to avoid making the regular method slower:
C#
public void PublishNew(Type eventType)
{
    if (subscriberLookup.TryGetValue(eventType, out object subject))
    {
        object @event = Activator.CreateInstance(eventType);
        Type t = typeof(IObserver<>).MakeGenericType(eventType);
        MethodInfo onNext = t.GetMethod("OnNext");
        onNext.Invoke(subject, new[] { @event });
    }
}

...

eventMessager.PublishNew(w[0]);




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


modified 5-Sep-18 13:17pm.

GeneralRe: Problems with Rx EventMessager Pin
Kenneth Haugland5-Sep-18 9:49
mvaKenneth Haugland5-Sep-18 9:49 
GeneralRe: Problems with Rx EventMessager Pin
Kenneth Haugland5-Sep-18 9:56
mvaKenneth Haugland5-Sep-18 9:56 
QuestionUnable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418904-Sep-18 22:55
Member 139418904-Sep-18 22:55 
AnswerRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Richard MacCutchan4-Sep-18 23:10
mveRichard MacCutchan4-Sep-18 23:10 
GeneralRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418904-Sep-18 23:19
Member 139418904-Sep-18 23:19 
AnswerRe: Unable to Access Method in Referenced .dll (C#/VS2017) Pin
Member 139418905-Sep-18 0:24
Member 139418905-Sep-18 0:24 
QuestionConstructor Injection Pin
jones298474-Sep-18 7:33
jones298474-Sep-18 7:33 
AnswerRe: Constructor Injection Pin
Mc_Topaz4-Sep-18 10:15
Mc_Topaz4-Sep-18 10:15 
QuestionAforge Video-Picture quality Pin
User 136751144-Sep-18 1:08
User 136751144-Sep-18 1:08 
QuestionC# convert image format to dpx Pin
Member 135795963-Sep-18 23:28
Member 135795963-Sep-18 23:28 
AnswerRe: C# convert image format to dpx Pin
OriginalGriff4-Sep-18 1:10
mveOriginalGriff4-Sep-18 1:10 
SuggestionRe: C# convert image format to dpx Pin
Richard Deeming4-Sep-18 1:48
mveRichard Deeming4-Sep-18 1:48 
Question"Hiding" functions from StackTrace Pin
Bernhard Hiller3-Sep-18 2:37
Bernhard Hiller3-Sep-18 2:37 
AnswerRe: "Hiding" functions from StackTrace Pin
OriginalGriff3-Sep-18 4:12
mveOriginalGriff3-Sep-18 4:12 
AnswerRe: "Hiding" functions from StackTrace Pin
Gerry Schmitz4-Sep-18 4:53
mveGerry Schmitz4-Sep-18 4:53 
Questionhow to use array in main that was created in class? Pin
Member 139703232-Sep-18 8:51
Member 139703232-Sep-18 8:51 
AnswerRe: how to use array in main that was created in class? Pin
Luc Pattyn2-Sep-18 16:40
sitebuilderLuc Pattyn2-Sep-18 16:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.