using duplicated class

A

Andrus

Two assemblies both referenced by application contain same class (created by
script):

namespace Script
{
public class Class1 { }
}


Compiling application

class Program
{
static void Main()
{
var t = new Script.Class1();
}
}


causes error

The type 'Script.Class1' exists in both
'c:\myapp\Project1\bin\Release\Project1.dll' and
'c:\myapp\Project2\bin\Release\Project2.dll'


How to force compiler to use specific assembly, like

var t = new Script.Class1@",
Assembly=C:\Users\Andrus\Temp\TempAssembly.dll"
();

Andrus.
 
P

Pavel Minaev

Two assemblies both referenced by application contain same class (createdby
script):

namespace Script
{
    public class Class1 { }

}

Compiling application

class Program
{
    static void Main()
    {
        var t = new Script.Class1();
    }

}

causes error

The type 'Script.Class1' exists in both
'c:\myapp\Project1\bin\Release\Project1.dll' and
'c:\myapp\Project2\bin\Release\Project2.dll'

How to force compiler to use  specific assembly, like

 var t = new Script.Class1@",
Assembly=C:\Users\Andrus\Temp\TempAssembly.dll"
();

Go into Properties of each reference (in Solution Explorer, expand
References under your project, right-click on a specific reference,
and select Properties). There is a property called "Aliases" - change
it so that it is different for references to Project1 and to Project2
- simplest is just to use "Project1" and "Project2". Now you can use
the "extern alias" directive in the .cs file where you need to
disambiguate:

extern alias Project1;
extern alias Project2;

and disambiguate as needed using double-colon operator:

Project1::Script.Class1
Project2::Script.Class2
 
M

Marc Gravell

Pavel has given the answer to the specific question (extern alias),
but note that it is worth trying to avoid this scenario where-ever
possible. Perhaps consider making the namespace specific for each
script? So you have

Project1.Script.Class1 in Project1.dll
and
Project2.Script.Class1 in Project2.dll

Much simpler and more maintainable.

Marc
 
A

Andrus

Marc,
Perhaps consider making the namespace specific for each
script? So you have

Project1.Script.Class1 in Project1.dll
and
Project2.Script.Class1 in Project2.dll

Much simpler and more maintainable.

I'm planning to allow end users to override entity objects for custom
default values and validation using scripting.
Custom entity classes are created dynamically by entity factory. For
example, user can create the following script:

namespace MyApp.Script {
public class Invoice : MyApp.Business.Invoice {
public override void OnCreated(object sender, CreateEventArgs e) {
// set default customer for new invoice
this.CustomerId = "123";
}
}
}

Another, Query creation script which returns only some properties which we
discussed earlier refers to this object by using "MyApp.Script.Invoice".

User can also modify script at runtime, for example change CustomerId to
"456".
In this cases entity factory creates new type but it has same namespace and
name:

After that compiler error occurs in query creation script since script
engine adds references to all assemblies referenced by application to
compiler.

Possible solutions:

1. How I can use global aliases or different namespaces in this case ?

2. Create all other class creation scripts so that they take entity type as
generic type parameter.
In this case application can pass entity to type created by script using
MakeGenericType()

3. Should I create Linq expression insted of script ? I need to modify your
earlier selected property selection sample but
I do'nt know how to make such modification.

4. Should I use delegates and static methods insted of entity class dynamic
override ?

5. Modify script engine to add reference only to latest entity assembly to
compiler.

I don't know which is best solution. I will try solution 2 first since this
seems to be simplest to implement.

Andrus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top