Click here to Skip to main content
15,892,253 members
Home / Discussions / C#
   

C#

 
QuestionDrag and drop user-control from stack-panel to Windows Explore(or Desktop) for saving it to PNG in C# wpf Pin
Member 133785039-Sep-18 0:09
Member 133785039-Sep-18 0:09 
AnswerRe: Drag and drop user-control from stack-panel to Windows Explore(or Desktop) for saving it to PNG in C# wpf Pin
Eddy Vluggen9-Sep-18 0:22
professionalEddy Vluggen9-Sep-18 0:22 
GeneralRe: Drag and drop user-control from stack-panel to Windows Explore(or Desktop) for saving it to PNG in C# wpf Pin
Member 133785039-Sep-18 1:13
Member 133785039-Sep-18 1:13 
GeneralRe: Drag and drop user-control from stack-panel to Windows Explore(or Desktop) for saving it to PNG in C# wpf Pin
Eddy Vluggen9-Sep-18 2:10
professionalEddy Vluggen9-Sep-18 2:10 
GeneralRe: Drag and drop user-control from stack-panel to Windows Explore(or Desktop) for saving it to PNG in C# wpf Pin
Member 133785039-Sep-18 8:09
Member 133785039-Sep-18 8:09 
GeneralRe: Drag and drop user-control from stack-panel to Windows Explore(or Desktop) for saving it to PNG in C# wpf Pin
Eddy Vluggen9-Sep-18 8:20
professionalEddy Vluggen9-Sep-18 8:20 
QuestionForms opening other forms Pin
Member 112336117-Sep-18 20:33
Member 112336117-Sep-18 20:33 
AnswerRe: Forms opening other forms Pin
OriginalGriff7-Sep-18 21:20
mveOriginalGriff7-Sep-18 21:20 
GeneralRe: Forms opening other forms Pin
Member 112336118-Sep-18 0:03
Member 112336118-Sep-18 0:03 
GeneralRe: Forms opening other forms Pin
Member 112336118-Sep-18 0:06
Member 112336118-Sep-18 0:06 
GeneralRe: Forms opening other forms Pin
OriginalGriff8-Sep-18 0:07
mveOriginalGriff8-Sep-18 0:07 
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 

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.