-1

I have an assembly stored in a byte array, and I want to use a Type that's in it.

I can save it to file while developing, and reference it in the project's references. But then when deployed - the file isn't there and the program crashes. If I don't reference it - Visual Studio complains that the Type does not exist, of course.

I'm trying to avoid using reflection if possible. This question implies that when the assembly is available beforehand - it should be simple.

The question is not how to load the assembly. Rather - How to use the available classes in it as any other code - with intellisense etc.

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
  • @L.B You caught me in mid-edit. Since the assembly already exists - there should be a way to use the Type more simply. – ispiro Aug 25 '14 at 17:19
  • @downvoters Care to comment? – ispiro Aug 25 '14 at 18:23
  • Not my downvote, but it is very hard to see what you trying to figure out: there is exactly one way for VS to know about classes from an assembly - to add reference to the project, so how to do it does not feel like a valid question on SO (especially for someone with 100+ rep). And you seem to rule out all other interpretations of the question. Side note: likely most readers will consider `Assembly.LoadXXX` as reflection, so "avoid reflection" in such scenario feels very confusing. – Alexei Levenkov Aug 25 '14 at 18:50
  • @AlexeiLevenkov "there is exactly one way for VS to know about classes from an assembly - to add reference to the project" - I want to reference it, but then "detach" is somehow, so that later the application will continue working with the assembly from the byte array (which is _exactly_ the same as the one used for developing.) I get the feeling, though, that that's impossible. If so - that's the answer - that it's impossible. Thanks for your input. – ispiro Aug 25 '14 at 19:11
  • I'm still not sure what exactly your question (as most of what I'd consider interesting part is cut off by "The question is not how to load the assembly"). How you scenario is different from using something like System.dll (or any other assembly that is not directly deployed by you)? Maybe you are looking for setting "copy local" property to "false" in the project? – Alexei Levenkov Aug 25 '14 at 19:46

1 Answers1

1

I think You can use static AssemblyResolve event.

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
    {
        //Use e.Name
        //Load assembly form byte[] and return it.
        return Assembly.Load(byteArray);
    };
EZI
  • 15,209
  • 2
  • 27
  • 33
  • I _can_ load the assembly. What I want is to be able to use a `Type` in it simply. – ispiro Aug 25 '14 at 17:25
  • 2
    @ispiro Just reference the dll in your project as always, but you don't have to deploy it. It can be ,for ex, a stream in your resource. – EZI Aug 25 '14 at 17:26
  • Thanks. That seems like a good solution. I need to look into that. – ispiro Aug 25 '14 at 17:27
  • It seems I'll still need that resource when deploying. Is there any way I can use a simple byte array (say, a base64 string in a regular `cs` file)? – ispiro Aug 25 '14 at 17:34
  • @ispiro - If you want to *refer* to the type in your code at design/compile time, then you need to add a *reference* to it. You can have your app *use* the assembly, but then you'd have to write your own logic to use the types dynamically (thru reflection). – hometoast Aug 25 '14 at 18:29
  • @ispiro - please consider asking separate question instead of comments (question states "I have an assembly stored in a byte array", but your comment to this answer hints that you don't really have the byte array...) – Alexei Levenkov Aug 25 '14 at 18:44
  • @AlexeiLevenkov I don't understand your comment. That's just a graphical example of how I would have it in a byte array. – ispiro Aug 25 '14 at 19:14