dynamically loaded .net assembly does not work in C# -



dynamically loaded .net assembly does not work in C# -

i need run .wav file in windows app using only mediaplayer class , need load assemblies require mediaplayer class @ runtime. need utilize presentationframework.dll,windowsbase.dll , presentationcore.dll. object creation of mediaplayer when trying invoke mediaplayer constructor.. fails. below code :

assembly assembly = assembly.loadfrom("presentationcore.dll"); type type = assembly.gettype("system.windows.media.mediaplayer"); object instanceofmytype = activator.createinstance(type); type.invokemember("mediaplayer", bindingflags.declaredonly | bindingflags.public | bindingflags.nonpublic | bindingflags.instance | bindingflags.setproperty, null, instanceofmytype,null); type.invokemember("open", bindingflags.invokemethod | bindingflags.instance | bindingflags.public, null, instanceofmytype, new object[] { new uri("c:\\mymusic\\song1.wav") }); type.invokemember("play", bindingflags.invokemethod | bindingflags.instance | bindingflags.public, null, instanceofmytype, null);

but not work .. gives error @ line

type.invokemember("mediaplayer", bindingflags.declaredonly | bindingflags.public | bindingflags.nonpublic | bindingflags.instance | bindingflags.setproperty, null, instanceofmytype,null);

do need load assemblies or presentationcore.dll required. not user how run this.

reference used -> http://edu-kinect.com/blog/2014/07/10/reflection-examples-c/

thanks,

considering sequence works (you need windows media player > 10 on winxp or media features enabled in later oses), not mentioning it's bad thought @hanspassant mentioned:

var mediaplayer = new system.windows.media.mediaplayer(); mediaplayer.mediafailed += (o, ev) => debug.writeline("media failed: {0}", ev.errorexception.message); mediaplayer.mediaopened += (sender, e) => debug.writeline("media opened"); //string mediafilepath = uri uri = new uri(mediafilepath, urikind.relativeorabsolute); mediaplayer.open(uri); mediaplayer.play();

then reflecting above sequence should work too:

type type = type.gettype("system.windows.media.mediaplayer, presentationcore, version=4.0.0.0, culture=neutral, publickeytoken=31bf3856ad364e35"); object instancemediaplayer = activator.createinstance(type); // string mp3filepath = ...; var uri = new uri(mp3filepath, urikind.relativeorabsolute); bindingflags bfmethodcall = bindingflags.invokemethod | bindingflags.instance | bindingflags.public; type.invokemember("open", bfmethodcall, null, instancemediaplayer, new object[] { uri }); type.invokemember("play", bfmethodcall, null, instancemediaplayer, null);

moreover see using wav file source. if need play uncompressed wav files (no other media formats or compressed wavs) , don't want command volume or other playback aspects more lightweight soundplayer should suffice, without loading references.

var soundplayer = new system.media.soundplayer(); soundplayer.soundlocation = @"pathto.wav"; soundplayer.load(); soundplayer.play();

c# .net load runtime assemblies

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -