Add reference to project in c# - c#

I have some strange problem. I have a solution with the following structure
http://i33.tinypic.com/10fbzbq.jpg
As you can see when i wonna import the VDB.Common.RequestAndResponses it gives an error.
The namespace of dat Class Library is VDB.Common.RequestAndResponses.
I am new in c# , so it could be that i forgot something stupid.

I strongly suspect that Base.cs (the only C# file shown in the VDB.Common.RequestAndResponses project) doesn't actually declare a type in the VDB.Common.RequestAndResponses namespace - or that it only declares an internal (rather than public) type.
For example, note that the code you're creating is under the VDB.Client.Infrastructure project, but is only declaring a class in the Agatha namespace - not VDB.Client.Infrastructure.Agatha, which may be what you were intending. Do you have the same kind of thing in Base.cs, perhaps?
Without seeing the code in Base.cs, we can't see what's wrong though. If you could just post a snippet of that - just the namespace and class declaration - that would be helpful.
Note that although a class library has a default namespace, this isn't prepended to whatever the source file actually declares. In other words, in a library of Acme.Widgets, if you had a declaration of:
namespace Web
{
public class Button {}
}
that would only declare the type Web.Button, not Acme.Widgets.Web.Button.
EDIT: The OP's "answer" confirms what I thought... basically it's not declaring a namespace at all. It should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agatha.Common;
namespace VDB.Common.RequestAndResponses
{
public abstract class BaseRequest :Request
{
// Code
}
public abstract class BaseResponse : Response
{
// Code
}
}
I would also strongly advise that these classes should be put in two separate files, BaseRequest.cs and BaseResponse.cs. I'm also pretty surprised to see a reference to Agatha.Common - shouldn't that be VDB.Common.Agatha or something like that?

Right click on the "VDB.Common.RequestAndResponses" reference in solution explorer and choose "Show in object browser", make sure the namespace is found there with the exact spelling and capitalization.

Try to use the Base class in the client code and hover over it and allow the Visual Studio IDE to prompt you to add the appropriate namespace. The namespace defined in the Base class could be different to what you think.
EDIT
As Jon as demonstrated in the 2nd part of his answer - the name of the code file does not automatically correspond to the namespace.

The Base.cs file looks like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agatha.Common;
public abstract class BaseRequest :Request
{
public string UserName { get; set; }
public string UserDomainName { get; set; }
public string ClientLanguageCode { get; set; }
public DateTime ClientCreated { get; set; }
public DateTime ClientSent { get; set; }
public DateTime ServerReceived { get; set; }
public DateTime ServerProcessed { get; set; }
public void BeforeSend(IUserContext context)
{
ClientSent = DateTime.UtcNow;
UserName = context.UserName;
UserDomainName = context.UserDomainName;
ClientLanguageCode = context.LanguageCode;
}
}
public abstract class BaseResponse : Response
{
public DateTime ServerCreated { get; set; }
public DateTime ServerProcessed { get; set; }
public string[] ValidationErrors { get; set; }
public bool IsValid
{
get { return Exception == null & !ValidationErrors.Any(); }
}
}

Related

Cannot apply attribute class 'JsonConverter' because it is abstract

I'm using Newtonsoft JSON library and I'm trying to deserialize a JSON. The problem is that when I use [JsonConverter(typeof(StringEnumConverter))] I get this error: Cannot apply attribute class 'JsonConverter' because it is abstract.
Here are my classes:
public class ActionRepository
{
[JsonConverter(typeof(StringEnumConverter))]
public enum AllowedActions
{
FINDWINDOW,
}
public enum AllowedParameters
{
WINDOWNAME,
}
}
public class Action
{
public AllowedActions Name { get; set; }
public List<Parameter> Parameters { get; set; }
}
I get the squiggly line under the JsonConverter.
EDIT: The JsonConverter class is indeed abstract if I navigate to the class (ctrl+click in VS). I'm using .NET for Windows Universal.
The problem appears to be that when not targeting a .Net framework application - the JsonConverter class is marked as abstract.
The solution looks to be to use JsonConvert as an alternative.

Reinforced.Typings does not produce FQN for types with UseModules(true, false)

Summary:
I have a simple solution with two projects, e.g. ProjA and ProjB. In ProjA I have specified my custom the RTConfigure method for fluent configuration of the translation process using the Reinforeced.Typings tool for translating C# to TypeScript code. In ProjB I defined C# ViewModel classes that are intended for translation.
Problem:
In ProjB I defined three classes in three separate files and each of them belongs to their specific namespace. When using a global builder config by calling e.g. builder.UseModules(true, false) the produced code does not include fully qualified namespace names for the translated types that refer to types after the keyword extends and for types that are specified as members of another class (which belongs to another namespace different from the one of the used member type).
For example:
ProjB.Ns1.Class1
ProjB.Ns2.Class2
ProjB.Ns3.Class3
All three classes defines just two simple props, where Class3 differs a bit by containing one extra property with the type of Class1. Class2 inherits from Class1.
Here is the example code:
// Class1.cs
namespace ReinforcedTypings.DifferentNamespaces.Ns1
{
public class Class1
{
public int AnIntegerPropNs1 { get; set; }
public string AStringPropNs1 { get; set; }
}
}
// Class2.cs
using ReinforcedTypings.DifferentNamespaces.Ns1
namespace ReinforcedTypings.DifferentNamespaces.Ns2
{
public class Class2 : Class1
{
public int AnIntegerPropNs2 { get; set; }
public string AStringPropNs2 { get; set; }
}
}
// Class3.cs
using ReinforcedTypings.DifferentNamespaces.Ns1
namespace ReinforcedTypings.DifferentNamespaces.Ns3
{
public class Class3
{
public int AnIntegerPropNs3 { get; set; }
public string AStringPropNs3 { get; set; }
public Class1 AClass1PropNs3 { get; set; }
}
}
The configure method is very simple and is defined as follows:
public static class RTConfig
{
public static void Configure(ConfigurationBuilder builder)
{
var types = typeof(ReinforcedTypings.DifferentNamespaces.Ns1.Class1).Assembly.GetTypes().ToList();
builder.ExportAsClasses(types.Where(x => x.IsClass), c => c.WithAllFields().WithAllMethods().WithAllProperties());
builder.Global(c => c.UseModules(true, false));
// ^ commenting out this line will also cause a different error
}
}
I use the .xml configuration file to specify that the translation should all result in one file by setting the following option (false) as:
<RtDivideTypesAmongFiles>false</RtDivideTypesAmongFiles>
...and this results in the following TypeScript code in (translated.ts as set in the .xml configuration file):
export namespace ReinforcedTypings.DifferentNamespaces.Ns3 {
export class Class3
{
public AnIntegerPropNs3: number;
public AStringPropNs3: string;
public AClass1PropNs3: Class1; // <- error here
}
}
export namespace ReinforcedTypings.DifferentNamespaces.Ns2 {
export class Class2 extends Class1 // <- error here
{
public AnIntegerPropNs2: number;
public AStringPropNs2: string;
}
}
export namespace ReinforcedTypings.DifferentNamespaces.Ns1 {
export class Class1
{
public AnIntegerPropNs1: number;
public AStringPropNs1: string;
}
}
Please notice that Class1 type is missing the full namespace notation for its name after extends and in the translated property member private AClass1PropNs3: Class1; of the Class3.
This causes an issue in TypeScript as these types are not found due to not specifying the full (namespace) path name of the type.
Side issue observed:
When not working with UseModules, if you comment out that line, another issue will be found in the translated TypeScript code and it is due to the order of translated code, i.e. the Class1 will not be found in Class3 because it is used before its declaration.
Conclusion:
I did not go into the source of RT but I doubt there is an one issue with the UseModules(true, false) where second argument explicitly states not to discard the namespaces and another issue regarding the order of the code that gets outputted. Anyways, having the second argument value set to false one would expect to always and everywhere have the FQN for the used type. If I am mistaken of the usage of this config prop, then I propose it would be of great benefit to have such global config which would enforce the usage of FQN for all/desired types.
Definitely FQN issue is bug of RT and is fixed in version 1.4.6.
About side issue: see this GitHub Discussion

C# public property get accessor inaccessible from another namespace

I have tried making the field public itself; I have also tried using public get, even if, as I understand, access modifiers inside a property will only have effect if more restrictive. Yet I wasn't able to access the 'problem.Points'(last line) property from the TestUnit. I get an "get accessor inaccessible" alert. Notice that I'm able to access it from another class in the same namespace. I must be missing something very basic here.
namespace Coordinates_Path
{
public interface IProblem
{
abstract public List<Node> Points { get; set; }
abstract public Object GetStartState();
abstract public bool IsGoalState();
abstract public Object GetSuccessor();
}
public class ShortestPathThroughCoordinates : IProblem
{
private Node startState;
private List<Node> points;
public List<Node> Points { get { return points; } private set; }
//...
//...
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Coordinates_Path;
using System.Linq;
using System.Collections.Generic;
namespace CoordPathTest
{
[TestClass]
public class KruskalTest
{
[TestMethod]
public void TestMST()
{
// ...
IProblem problem = new ShortestPathThroughCoordinates("P1", coordDic);
MSTKruskal kruskal = new MSTKruskal(problem.Points)
If you look at
public class ShortestPathThroughCoordinates : IProblem
{
public List<Node> Points { get { return points; } private set; }
...
all referenced classes must be visible to the calling assembly. Check to ensure that Node is also visible.
Change your interface to this:
public interface IProblem
{
List<Node> Points { get; set; }
Object GetStartState();
bool IsGoalState();
Object GetSuccessor();
}
Interfaces only define public members so you do not have to declare it. All members of an interface must be implemented so there is also no need to declare them as abstract.
Unless private List points; is set somewhere further down in the code that we can't see you are never initializing this variable so your get will be null;

C# Runtime Compilation - Add new interface to namespace

I am currently confused, if it is possible to compile an interface at runtime and add it to a "hardcoded" namespace.
Some code to explain:
using System;
using System.Runtime.CompilerServices;
using EngineCore;
namespace EngineCore
{
public interface iMobilePhones
{
string phoneNumber {get; set;}
string phoneRingtone {get; set;}
void CallNumber(string phoneNumber);
}
}
This is the runtime compiled interface, I want to add to the EngineCore namespace at runtime.
The compilation seems to work as expected and as you can see I compiled it using the EngineCore namespace.
using System;
using System.Runtime.CompilerServices;
using EngineCore;
namespace EngineCore
{
public class EQMobilePhoneCheap : iMobilePhones
{
//iMobilePhones interface members
private string PhoneNumber;
private string PhoneRingtone = "Default";
public string phoneNumber
{
get
{
return PhoneNumber;
}
set
{
PhoneNumber = value;
}
}
public string phoneRingtone
{
get
{
return PhoneRingtone;
}
set
{
PhoneRingtone = value;
}
}
//iMobilePhones interface functions
public void CallNumber(string phoneNumber)
{
}
}
}
This is the second runtime compiled code. As you can see, I am trying to use the iMobilePhones interface I compiled before.
But the compilation fails because iMobilePhones is not an interface of EngineCore
The actual question:
So I am wondering if there is a way to "register" the previously compiled interface iMobilePhones to the EngineCore namespace?
Thank you very much for reading.
Any suggestions are welcome.
You simply need to add a reference to the assembly containing the interface when you compile code that uses it.

How to use nested class in WPF XAML?

I am refactoring the code from sample:
24.129.21. Master Detail Binding
from C# / CSharp Tutorial » Windows Presentation Foundation » Binding)
And after excluding Skills class, with corresponding changes in
in MainWindow.xaml
<local:Team>
<local:Employee Name="Larry" Age="21">
<local:Employee.Skills>
<!-- local:Skills -->
<local:Skills>
in MainWindow1.xaml.cs:
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
namespace WpfApplication
{
public class Skill
{//I'd like to exclude class Skill having moved it into class Employee as nested one
public string Description { get; set; }
}
public class Employee
{
public string Name { get ; set; }
public int Age { get; set; }
public List<Skill> Skills { get; set; }
public Employee()
{
Skills=new List<Skill>();
}
/*class Skill
{
public string Description { get; set; }
} */
}
public class Team : ObservableCollection<Employee> { }
public class Company
{
public string CompanyName { get ; set; }
public Team Members { get ; set; }
}
public class Companies : ObservableCollection<Company> { }
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
}
How should I change Window1.XAML if to move:
Skill class into Employee class
in Window1.xaml.cs?
Related questions
based on the same code:
How to change a custom type to System type (string) in WPF XAML?
Update (answering 1st RV1987's comment):
Answers tp Creating an instance of a nested class in XAML tell that it is possible but unclear how to use:
answer by Ludovic tells that it is possible but contains comment that it is not clear how to use.
This is quite in line with my experience and this question
another answer by townsean is based on citation from msdn:
"Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties."
But, it is in general, and for "your custom class" but in in my concrete code attached to this question there are dozens "dots" (like Employee.Skills) and it is not my custom class that is nested but my custom class has nested class inside.
Update2 (answering 2nd RV1987's comment-question):
Yes, I've just tried that + approach, which does not work, but:
XAML gives me errors even on perfectly working elements
I've not tried to use reflector myself or find any other workable approach or less ambiguous reference from Microsoft on it
Unfortunately, what you want to do is not possible in XAML (from MSDN):
Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.

Categories