55// Created: 2013.08.20
66
77using System ;
8+ using System . Collections . Generic ;
89using System . Linq ;
910using Mono . Cecil ;
1011
@@ -115,12 +116,15 @@ private bool TryGetOrmAssembly(ProcessorContext context, out AssemblyNameReferen
115116 assemblyNameReference = context . TargetModule . AssemblyReferences
116117 . FirstOrDefault ( r => comparer . Equals ( r . FullName , WellKnown . OrmAssemblyFullName ) ) ;
117118
118- if ( assemblyNameReference != null )
119+ if ( assemblyNameReference != null ) {
119120 return true ;
121+ }
120122
121- // check whether project had reference to Xtensive.Orm assembly.
122- if ( ! context . Configuration . ReferencedAssemblies . Reverse ( ) . Any ( ra => ra . EndsWith ( WellKnown . OrmAssemblyShortName + ".dll" , StringComparison . InvariantCultureIgnoreCase ) ) )
123+ // check whether project had reference to Xtensive.Orm assembly
124+ // but omitted during project compilation as not used.
125+ if ( ! IsOrmReferenceOmmitedByCompiler ( context . Configuration . ReferencedAssemblies ) ) {
123126 return false ;
127+ }
124128
125129 // if target module has't got reference to the assembly and there was reference to the assembly in project
126130 // then probably some of target module referenses references the assembly.
@@ -131,8 +135,27 @@ private bool TryGetOrmAssembly(ProcessorContext context, out AssemblyNameReferen
131135 . SelectMany ( m => m . AssemblyReferences )
132136 . FirstOrDefault ( ar => comparer . Equals ( ar . FullName , WellKnown . OrmAssemblyFullName ) ) ;
133137
134- if ( assemblyNameReference != null )
138+ if ( assemblyNameReference != null ) {
139+ context . ShouldAddReferenceToOrm = true ;
135140 return true ;
141+ }
142+ return false ;
143+ }
144+
145+ private bool IsOrmReferenceOmmitedByCompiler ( IList < string > projectReferences )
146+ {
147+ if ( projectReferences . Count == 0 ) {
148+ return false ;
149+ }
150+ var lastItemIndex = projectReferences . Count - 1 ;
151+
152+ // Orm assembly tends to be at the end of the list
153+ string searchCreteria = WellKnown . OrmAssemblyShortName + ".dll" ;
154+ for ( int i = lastItemIndex ; i >= 0 ; i -- ) {
155+ if ( projectReferences [ i ] . EndsWith ( searchCreteria , StringComparison . OrdinalIgnoreCase ) ) {
156+ return true ;
157+ }
158+ }
136159 return false ;
137160 }
138161
0 commit comments