problem with adding Microsoft.WindowsMobile.Status - c#

when i add Microsoft.WindowsMobile.Status; reference and code
private SystemState displayRotationState = new SystemState(SystemProperty.DisplayRotation);
i get
Error 1 The type 'Microsoft.WindowsMobile.IApplicationLauncher' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.WindowsMobile, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. C:\Projekti\Skladiscenje\Skladiscenje\Skladiscenje\CustomForms\NonFullscreenForm.cs 12 25 Skladiscenje
what is wrong and how to solve it?

As the error clearly tells you, you should also add a reference to the assembly:
Microsoft.WindowsMobile
after you do this it should work, ops, compile at least :)

Related

FluentAssertions error CS0012: You must add a reference to assembly System.Data

I added the nuget FluentAssertions 6.7.0 in a test project using .NET Framework 4.6.1. I run tests from Rider 2022.1.1.
I'm new to this nuget and I read the intro and searched for issues (none found). I come from the Should family and trying to upgrade.
I cannot build with basic assertions. Here is the initial code:
using FluentAssertions;
using Moq;
using System;
using Xunit;
public class MyTestClass
{
[Fact]
public void GetProvider_ByRemoteName_Works()
{
// input
var desiredRemoteName = "Remote2";
// prepare
var context = Context.New(); // mocks and stubs
// execute
var result = context.SomeService.GetProvider(desiredRemoteName);
// verify
result.Should().NotBeNull(); // error line
result.Should().BeOfType<MyProviderClient>(); // error line
}
The build errors are:
error CS0012: The type 'DataTable' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
error CS0012: The type 'DataColumn' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
...
error CS0012: The type 'DataRow' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
I don't understand why I should reference this "System.Data" assembly. That does not seem legit. If I do reference it:
MyTestClass.cs: [CS0121] The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should(TDataRow)' and 'DataSetAssertionExtensions.Should(TDataSet)'
Also, removing the error lines and using line provide a valid build and test run.
Also, the IDE editor indicates:
The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should(TDataRow)' and 'DataSetAssertionExtensions.Should(TDataSet)'
Also, using Xunit's assertions works:
// verify
Assert.NotNull(result);
Assert.IsType<MyProviderClient>(result);
Following up on your comments, let's consider this updated code:
// execute
object result = context.SomeService.GetProvider(desiredRemoteName);
// verify
result.Should().BeAssignableTo<IMyInterface>()
.And.BeOfType<SomeImplementation>()
.Which
.Configuration
.Should() // error line
.NotBeNull();
The same error occurs on the latest .Should() call.
MyTestClass.cs: [CS0121] The call is ambiguous between the following methods or properties: 'DataRowAssertionExtensions.Should(TDataRow)' and 'DataSetAssertionExtensions.Should(TDataSet)'
Is it considered "normal" with FluentAssertions to do .BeOfType<>().Which everywhere? I feel something is wrong on my side or in the way the lib works.
As I recall, there's a weird issue with .NET Framework 4.6.1 where the assembly reference to System.Data wasn't automatically added. For newer framework versions, it happens automatically.
The IDE error you listed suggests that the compiler doesn't know which of the .Should() overloads to call. This isn't unusual for any library that relies on overload resolution.
Since it looks like you're only intending to test the return type in this context, you only "need" the ObjectAssertions overload of .Should(). As such, one way you could avoid the ambiguous invocation like this:
object result = context.SomeService.GetProvider(desiredRemoteName);
result.Should().BeOfType<MyProviderClient>() // Chained FA assertions are now typed as MyProviderClient
.Which.Should().BeEquivalentTo(new { Foo = "bar" });
Observe:
Remember that the value null does not have a runtime type, so asserting that an object has some specific runtime type implicitly asserts that it is not null. FA's .BeOfType<T>() assertion will fail with a specific message if a null reference is asserted on.

The type 'Expando' is defined in an assembly that is not referenced

I'm getting the following compile error:
The type 'Expando' is defined in an assembly that is not referenced.
You must add a reference to assembly 'Westwind.Utilities, Version=2.64.0.0,
Culture=neutral, PublicKeyToken=6f7d66a3bb7de652'.
The thing is, I have a reference to that version of the assembly... I think the problem is due to a class library.
My ClassLibrary has the following class:
public class Generic_AnswerFile : Expando { ... }
and my Test Console has the following class:
public class Specific_AnswerFile : Generic_AnswerFile { ... }
The Specific_AnswerFile will not compile. Any ideas why? Both projects are .Net 4.0 and have references to the correct .Net 4.0 version of Westwind.Utilities 2.64
The type Expando is defined in his assembly
that means only in the GAC. though you have reference to it.. but it would fail to load. To load it properly you need to define it in your config file under assembly tag
<add assembly="Expando, Version=1.0.0.0, Culture=neutral, PublicKeyToken=[MyPublicKeyToken]"/>

Instantiating a class produces ReflectionTypeLoadException at Assembly.GetTypes()

I discovered that after my application generates Telerik report
var result = new ReportProcessor().RenderReport("PDF", new InstanceReportSource { ReportDocument = new MyTelerikReport(data) }, null);
var stream = new MemoryStream(result.DocumentBytes);
return CreateHttpFileResponse("MyReport.pdf", stream, "application/pdf");
I am not able to get all types within CurrentDomain
var typesWithAttribute = (from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes() //error appears here
//some filtering logic
select t).ToList();
I am getting error
System.Reflection.ReflectionTypeLoadException: Unable to load one or
more of the requested types. Retrieve the LoaderExceptions property
for more information.
LoaderExceptions:
System.IO.FileNotFoundException: Could not load file or assembly
'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The
system cannot find the file specified. File name:
'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'
After some investigation I found that assembly that fails to load: Telerik.Reporting.OpenXmlRendering, Version=8.0.14.311, Culture=neutral, PublicKeyToken=a9d7983dfcc261be and that assembly doesn't exists in AppDomain.CurrentDomain.GetAssemblies() before I generate report (I assume that assembly loaded dynamically by Telerik.Reporting, Version=8.0.14.311, Culture=neutral, PublicKeyToken=a9d7983dfcc261be).
I could filter out that assembly as I don't need any types from that but I am a bit worried about fact of having assemblies in domain that cannot be loaded - seems a bit wrong to me.
Could someone explain what there happens? Is it my issue or that is fault of 3rd party library that doesn't load all required assemblies?
The issue is not the assembly but the Type coming from a dependent assembly that has not been loaded.
If the GetTypes method is called on an assembly and a type in that assembly is dependent on a type in an assembly that has not been loaded (for example, if it derives from a type in the second assembly), a ReflectionTypeLoadException is thrown.
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes(v=vs.110).aspx

Can't do Assembly.Load(String) with a referenced assembly unless I instantiate a class within that assembly first. How to solve?

I have a very strange problem here. It looks like unless I instantiate a class within an assembly I get an assembly not found error.
For example:
Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);
Throws a FileNotFound exception on Assembly.Load
This:
Assembly.Load("something.blah, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
new someClassInAssembly();
Type mqType = Type.GetType(query.Attribute(fullyQualifiedName + ", " + assemblyInfo);
Object mq = Activator.CreateInstance(mqType);
Works fine. Yes, even if it is instantiated after Assembly.Load, so it is clearly a problem during compilation. How do I explicitly make sure that the assembly is loaded and findable during runtime, is there a compilation setting somewhere, what do I need to do?
Make sure you're loading the assembly you think you're loading, by supplying the path:
AssemblyName an = AssemblyName.GetAssemblyName(filePath);
Assembly.Load(an);
Honestly, if its just a single reference or a handful, just add an explicit reference somewhere it will save you a lot of effort.
//Use a static constructor somewhere appropriate.
static someClass(){
new AssemblyYouCareAbout.Object();
}
The alternatives are along the lines of hauling dlls manually to the bin of your running process or to add the dlls to the gac. I'd rather use the not-so-elegant static constructor and move on.

Get Assembly Names in Current Application Domain

I want to get Assemblies Friendly Names in Current Application Domain and hence I wrote something like this :
static void Main(string[] args)
{
foreach (System.Reflection.Assembly item in AppDomain.CurrentDomain.GetAssemblies())
{
Console.WriteLine(item.FullName);
}
}
But the problem is this is the output what I got rather than what I desired to see :
mscorlib, Version=2.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e0
ApplicationDomains, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
Actually I was expecting those names :
alt text http://www.pixelshack.us/images/xjfkrjgwqiag9s6o76x6.png
Can someone tell me if there is something I mistook.
Thanks in advance.
Or the names I was expecting weren't assemblies ?
You won't always get the pretty namespace names when you use reflection, you get the real names of the assemblies.
You also won't get all referenced libraries, only the ones that CURRENTLY loaded. If you add "XmlDocument foo = new XmlDocument()" above your code, System.XML will show up.
static void Main(string[] args)
{
XmlDocument foo = new XmlDocument();
foreach (System.Reflection.Assembly item in AppDomain.CurrentDomain.GetAssemblies())
{
Console.WriteLine(item.FullName);
}
}
Output:
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
ConsoleApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
It's impossible to get list of all referenced assemblies during runtime. Even if you reference it in your visual studio project, if you don't use them, C# compiler will ignore them and therefore they won't make it into your output file (exe/dll) at all.
And for the rest of your assemblies, they won't get loaded until they are actually used.
AppDomain.CurrentDomain.GetAssemblies() gives you array of all loaded assemblies and this list could be very different from what you see in visual studio project.
foreach(var assem in AppDomain.CurrentDomain.GetAssemblies())
{
Console.WriteLine(assem.GetName().Name);
}
Assembly.GetName() returns an AssemblyName object which has a Name property. That's what you're looking for.
Either use Assemly.GetName().Name or use reflection to find the AssemblyTitleAttribute and use that value.

Categories

Resources