Reputation: 7278
I have a solution with two projects. I cannot add a reference of A in B because it creates circular dependency. For avoiding redundant code, I would like to use a function in one of my projects in the other project. Is that possible?
Upvotes: 3
Views: 733
Reputation: 16878
it creates circular dependency
This part is interesting. It is a indication of bad design probably. If this function is common, it should be moved to third assembly (as class library) called like Common etc. Then it can be referenced by other assemblies safely and what is better - purpose of it is clear.
Upvotes: 3
Reputation: 53958
No it isn't. A function, or a method, is contained in a type, which is contained in an assembly. If you haven't access to this assembly, you can't use the method of this type, because you cannot either create an instance of the type -if the type isn't static- or use the type's name -if the type is static-.
Update
I say that's not possible in the context you cannot add a reference of one project to the other.
In order you avoid to write the same code twice, I would suggest you create another project, whose output would be a dll and there add the class or the classes that would be used by both projects A and B. In the class or classes, which you will define in the new project you can define the common methods you want and later use them from both project A and B. That should be a good approach, because later you could use this assembly to othet projects. So you will not have to write the same code from the start or make copy/past.
Upvotes: 6
Reputation: 54761
Create a third project and use it for shared code between the two other projects.
Upvotes: 3
Reputation: 70314
Yes you can! You can have project A reference project B. That is not a circular dependency:
A -> B(f)
If, on the other hand, B already references A, well, maybe your function is in the wrong place and you need to do some refactoring first to achieve:
A <- B(f) => A(f) <- B
Another tactic is to create a third "library" assembly that both assemblies reference:
A -> C(f) <- B
Upvotes: 3