What assembly is SerializeableAttribute in? - c#

I'm trying to set up serialization for a class. When I added:
[Serializeable]
I got the error:
"The type or namespace name could not be found (are you missing a using directive or an assembly reference?)"
I added System.Runtime.Serialization.Formatters.Soap and System.Runtime.Serialization but they didn't work. I also tried to find the other assemblies listed on the documentation page. I only found mscorlib in the VS Reference Manager and it was already added.
I wrote the code like so:
[Serializeable]
public class Test { }
Edit: I also tried:
[Serializeable()]
public class Test { }
I expected the code to compile without errors.
Edit: fixed wrong name for an assembly

Per MSDN, the SerializableAttribute type can be found in the following assemblies:
System.Runtime.Serialization.Formatters.dll
mscorlib.dll
netstandard.dll
System.Runtime.dll
EDIT
It may be easier to try the below. There may be namespace conflicts.
[System.SerializableAttribute]
public class Test {}

Related

Why do I get compilation error when trying to use record type in C#?

EDIT: Thank you everyone! I have never upgraded to a newer version of
.NET and language version before. Thus didn't know about .csproj
configuration. Even though I did a research before posting a question
I was not able to find a solution myself. So, I just leave these two
links for further reference, perhaps this might help someone as well.
https://learn.microsoft.com/en-us/dotnet/standard/frameworks
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version
I have upgraded to .NET 5.0.301
And finally got around to try record type in C# 9.0
I wrote a simple code but got an error during compilation.
I use Visual Studio Code as an editor.
VS Code version 1.57.0
C# extension version 1.23.12
Here is my settings.json:
"editor.semanticHighlighting.enabled": true,
"csharp.semanticHighlighting.enabled": true,
"omnisharp.path": "latest"
Set up:
dotnet new sln -n "Test"
dotnet new console -n "TestProject"
dotnet sln Test.sln add .\TestProject\TestProject.csproj
My code:
using System;
namespace TestProject
{
class Program
{
static void Main(string[] args)
{
var person = new Person() { Name = "Tom" };
person.Name = "Bob";
Console.WriteLine(person.Name);
}
}
public record Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Problems:
CS0246 The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?)
CS0246 The type or namespace name 'record' could not be found (are you missing a using directive or an assembly reference?)
CS0548 '<invalid-global-code>.Person': property or indexer must have at least one accessor
CS1513 } expected
CS0116 A namespace cannot directly contain members such as fields or methods
CS1022 Type or namespace definition, or end-of-file expected
Any help much appreciated
Check your target framework and language version in your .csproj file. You should find something like:
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>
If you don't find these properties, add them inside the <PropertyGroup>...</PropertyGroup> tags. If they are set to older versions, change them.
If this does not solve your problem, you may have installed your .NET SDK incorrectly, or you may have installed it in a directory other than the default one and the VS Code C# extension is not able to find it.

Xamarin assembly conflicts with the imported type

So I was changing my Project name and also my folders etc. I followed guide from here How do I rename a Project Folder from within Visual Studio?. All seemed to work, I can launch my app, but I got some erros and lots of lots of warnings
Warning example:
Warning CS0436 The type 'ObservableObject' in 'C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\Base\ObservableObject.cs' conflicts with the imported type 'ObservableObject' in 'TodoScheduler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\Base\ObservableObject.cs'. Volaapp C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\Base\SelectableObject.cs 5 IntelliSense Active
And errors like this:
Error CS0234 The type or namespace name 'XamlFilePathAttribute' does not exist in the namespace 'Xamarin.Forms.Xaml' (are you missing an assembly reference?) Volaapp C:\Users\Godhaze\Documents\Volaapp\Volaapp\Volaapp\obj\Debug\TodoScheduler.Pages.MenuPage.xaml.g.cs 14 IntelliSense Active
The code of the error:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace TodoScheduler.Pages {
[global::Xamarin.Forms.Xaml.XamlFilePathAttribute("C:\\Users\\Godhaze\\Documents\\Visual Studio 2017\\Projects\\Volaapp\\Volaapp\\Volaapp\\Pa" +
"ges\\MenuPage.xaml")]
public partial class MenuPage : global::TodoScheduler.Controls.BasePage {
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private global::Xamarin.Forms.ListView listView;
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "0.0.0.0")]
private void InitializeComponent() {
global::Xamarin.Forms.Xaml.Extensions.LoadFromXaml(this, typeof(MenuPage));
listView = global::Xamarin.Forms.NameScopeExtensions.FindByName<global::Xamarin.Forms.ListView>(this, "listView");
}
}
}
So I think Its an problem with Assembly?
I Added Reference In the first place, so what could be the problem?
Also do I need to change my namespaces? My projects name of PCL is "Volaapp" But Im using namespaces like: TodoScheduler.Base, because I think its refers to the Assembly. I might misunderstood this!
I tried cleaning the solution then deleting bin,obj folders and then rebuilding it, but that dident help.
So what should I do? I really want to fix this case and I can even give the full code of this to fix :(
I had the same problem, and found out I was performing a font size change operation on an array of labels that contained an image by accident.

Visual Studio adding prefix to stated namespace

I'm getting a few:
'The type or namespace 'blah' does not exist in 'A.B.C' (are you missing an assembly reference?)
The code looks like:
namespace A
{
using B.C;
public class Blah()
{
//...
}
}
A and B are both assemblies that are included in the solution. Both use .NET 4.0. Assembly B does in fact have a namespace C. Assembly B is included as a reference in assembly A.
Why is it looking for A.B.C when the reference is B.C?
The problem was a typo in a different file than the ones with errors.
In another file I had:
namespace A.B.C
{
//...
}
I must have done an accidental Ctrl-P at some point?
The steps I took to find the offending file:
Open up the most recent file that I had changed
Examine the namespace
Repeat with the next most recent

Recompiling the OpenXmlSdkTool.Core DLL with a anon-method and delegate error

I'm doing research into the OpenXmlSdkTools v2.5 and had a sneak peak inside the OpenXmlSdkTools.Core.DLL and saved it as a c# Project with ILSpy.
While this question is active, here is the OpenXmlSdkTools.Core.DLL as a way to quickly reproduce the problem I'm encountering.
When I tried to compile the single class library project, I get two errors about a missng reference to assembly 'System.Xaml'. eg:
The type 'System.Windows.Markup.IQueryAmbient' is defined in an
assembly that is not referenced. You must add a reference to assembly
'System.Xaml
The type 'System.Windows.Markup.IUriContext' is defined in an assembly
that is not referenced. You must add a reference to assembly
'System.Xaml
So I added the ref.
After that I'm stuck on what I hope is the last compile error and I can't figure it out.
Cannot convert anonymous method to type 'System.Delegate' because it
is not a delegate
type C:\TFS\ABC\src\OpenXmlSdkTool.Core\DocumentFormat.OpenXml.Tools\ApplicationExtensions.cs
10
Here is the code:
using System;
using System.Windows;
using System.Windows.Threading;
namespace DocumentFormat.OpenXml.Tools
{
public static class ApplicationExtensions
{
public static void DoEvents(this Application application)
{
application.Dispatcher.Invoke(DispatcherPriority.Background, delegate
{
});
}
}
}
I'm stuck and puzzled that its a decompiled DLL that should be easy to re-compile again. Do you think by me adding the Xaml reference its caused this problem? And why would I need to add the Xaml reference if the Core.DLL is a class library project and ILSpy didn't included it in the csproj file?
I've looked at all the other questions on here with the same error but none of them really helped.
Update
When you add System.Xaml.dll as reference to your project. The interface is declared there. Here is the doc.
So now I'm in a Catch22, if I add the Xaml dll it will solve the first 2 errors but then it will cause this other error.
After reproducing the issue on my machine, I found this http://staceyw1.wordpress.com/2007/12/22/they-are-anonymous-methods-not-anonymous-delegates/ (referenced from Convert this delegate to an anonymous method or lambda).
Adding a cast to Action solved the issue
application.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate
{
});
But there are probably other solutions.

RazorEngine Razor.Parse(...) throws exception about ServiceStack and Markdown?

I ran this simple example from the website and I get the error below when it calls Razor.Parse. How can I fix this???
http://razorengine.codeplex.com/
string template = "Hello #Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });
error CS0234: The type or namespace name 'Markdown' does not exist in the namespace 'ServiceStack' (are you missing an assembly reference?)
Not sure why you've linked to http://razorengine.codeplex.com
The 'ServiceStack' error assumes you want to use the Markdown engine in ServiceStack in which case you should be referencing the RazorEngine.dll that comes with ServiceStack not the one in razorengine.codeplex.com if that's what is done here.
I would imagine one of two things has happened. Either in your configuration file, namespaces have been added within the <razorEngine> configuration section, or the AddNamespace method is being called somewhere to include namespace imports in the compiled template.
The net result, is that namespaces are added to the generated class file, but references are missing. RazorEngine will automatically reference any loaded assemblies in the AppDomain.

Categories