Getting CS1061 error on compile even though the property exists - c#

I have come across the most curious problem ever as .Net dev. I am compiling a library which has a newly added property DeviceID in the class of UserInfo. The library internally uses the type and it's new property just fine, but when I try and reference it from another library, the compiler kicks back a compiler error stating
'library.UserInfo' does not contain a definition for 'DeviceID' and no extension
method 'DeviceID' accepting a first argument of type 'library.UserInfo' could
be found
Even though my class definition looks like:
public class UserInfo
{
public static UserInfo Current
{
get
{
if (UserInfoPrincipal.Current != null)
{
return UserInfoPrincipal.Current.UserData;
}
else
{
return null;
}
}
}
public string UserID { get; set; }
public string DeviceID { get; set; }
public string MikeLiUserID { get; set; }
public string TransactionServer { get; set; }
public string ApplicationKey { get; set; }
public string IpAddress { get; set; }
}
The offending code reads as such:
internal LogDetail BuildLogDetail(LogType entryType, string message)
{
return new LogDetail
{
ActingUserID = UserInfo.Current.UserID,
ActingDeviceID = UserInfo.Current.DeviceID,
ApplicationKey = UserInfo.Current.ApplicationKey,
IpAddress = UserInfo.Current.IpAddress,
EntryType = entryType,
OwnerID = UserInfo.Current.UserID,
LogData = message
};
}
I'd like to note that all of the other members of the UserInfo class go through the compiler correctly and it is just the DeviceID, which was added today, is causing the issue. I've tried Clean All, I've tried refreshing everything from TFS, manually deleting the obj and bin directories of both projects... nothing yet has worked.
UPDATE: This code, which is part of the library, works correctly:
public class UserInfoPrincipal : IPrincipal
{
public static UserInfoPrincipal Current
{
get
{
if (Thread.CurrentPrincipal is UserInfoPrincipal)
return (UserInfoPrincipal)Thread.CurrentPrincipal;
else
return null;
}
}
...
internal UserInfo UserData
{
get { return _userInfo; }
}
public string DeviceID
{
get { return _userInfo.DeviceID; }
}
...
}

So my hail mary pass was to remove the project reference and then add it again. Then it compiled. Have no clue why that worked, but figured I'd post it here for other who might run into the same problem.

Is the other library using a project reference or a binary reference? If its a binary reference, are you sure its using the latest build?

Check the reference path of the project that's generating the error; make sure you're either referencing the library project (if it's part of your solution) or the most recent build of the library (if it's not.)

I've gotten stuck in a few situations like this before. Here's what worked for me:
Are those two samples of code in separate projects? If so, I would say to try rebuilding the first project (containing the UserInfo class), then take out the line that fails the compilation out and try rebuilding the second project. Then do a rebuild all. Then add the offending line back in and do a rebuild all.
May not work for you, but worth a shot. I know that situation is frustrating.

for me -- try to recreate the line that shows an issue. Write the name of the object period (.) and wait for VS to show you the list of available properi

I encountered a very similar problem.
In my case I have a piece of code that I only need to run a couple times a year. When I attempted to use it there was an error accessing a Member. Nothing should have changed since the last time I used the code. Intellisense was detecting the member when using the '.' in Visual Studio. I restarted Visual Studio and the computer but the problem stayed.
In the end to fix my problem, I created a new file, copied the code from the original to the new file, and that was it. No code modifications. I used Ctrl-C, Ctrl-V so the content wasn't corrected by a manual touch. This isn't the first time copy and paste has fixed a bug so it's worth keeping the idea in the tool chest. Sometimes a mysterious problem demands an equally mysterious solution.

In my case, it was a problem with the web application's project properties. To fix it, I did the following:
Right-click the project > click Properties.
On the Build tab, change the Output path value for all configurations to: bin\
Previously, my output path had been bin\Debug or bin\Release depending on which configuration I was looking at. I don't know why this screwed with my markup page's ability to see methods in my codebehind, but it did. Once I changed this, the error disappeared.
This was in VS2012 w/ update 2.

My solution: I was create another name method what set property. My problem was on VS2015 + Silverlight 5

Related

VS 2015 C# AsyncExtension.cs not found

I haven't been able to find any information on this online. I'm debugging an console application, trying to step through some code. When I go to step over I get a source not found error. It says "AsyncExtension.cs not found" and then gives me some details. It says "You need to find AsyncExtension.cs to view the source for the current call stack frame". I'm working in VS2015. I'm assuming something async is happening behind the scenes, its erroring at some point but can't give me the specific details because it can't find the assembly containing AsyncExtension. But I don't know what this is, where to get it, etc. The code in particular I'm trying to step over is below. But I seem to get this at various points, and even when debugging other projects under the same solution.
Line of code:
var newObject = JsonConvert.DeserializeObject<HIDPMessage>(message.ToString());
HIDPMessage:
public class HIDPMessage
{
public string version { get; set; }
[Newtonsoft.Json.JsonProperty]
public string header { get; set; }
[Newtonsoft.Json.JsonProperty]
private Data Data { get; set; }
}
Not sure what you are trying to do but the code you have provided would not normally have any references to anything called AsyncExtension.cs. However your attempt to deserialize message could cause a JsonReaderException.
I'm guessing that "message" is some object that contains properties in common with HIDPMessage type and that you are trying to extract those into a new object, if so message.ToString(), unless overridden will just return the name of the type.
You need to serialize the object to a json string and use the json string instead of message.ToString();
Thanks for the input guys, you were right my code for deserializing was a little off. It turns out this app was built using VS2017 and some components from the Azure SDK were missing. I tried a manual install of the SDK but it wouldn't work - upgrading to 2017 fixed it, but I'm kinda surprised I had to upgrade just to get it to work.
I appreciate the feedback on the serialization stuff as well. This is a new-ish area for me and I'm still learning.

System.ArgumentException: Get Method not found for 'XXXXXXX' at System.Reflection.MonoPropert

whenever am trying to access the value of a property through reflection am getting a error like
"System.ArgumentException: Get Method not found for 'XXXX' at
System.Reflection.MonoProperty.Get"
. Am using Xamarin studio version 6.3 and property also declared as public only.
Model:
using System;
namespace XXXX.Core
{
public class ManufacturingParameters
{
public ManufacturingParameters()
{
}
private int oemCode;
public int OEMCode
{
get { return oemCode; }
set { oemCode = value; }
}
}
}
Ex Code:
ValueString = deviceInfoContentVC.devInfo.manufacturingParameters.GetType().GetProperty(ParamSchema.Key).GetValue(deviceInfoContentVC.devInfo.manufacturingParameters).ToString();
The above code works perfectly in Visual studio but not in xamarin studio.
But I can access the same property through below code:
ValueString = deviceInfoContentVC.devInfo.manufacturingParameters.oemCode.ToString();
Since i wanted to access it dynamically i need reflection kind of mechanism.
Is there any alternative solution/fix for this?
Thank You.
Finally resolved this issue and here is the solution I found.
It Was the linker stripping it away. I had changed the Linker Behaviour to "Link Framework SDK's Only" from "Link All" under iOS project's settings, and under the Build heading.
Now its works like a charm.

Winform app: Compiled App.Config-like file?

I would like to know if there is some kind of built-in compiled "App.Config" file?
The goal is to be able to have one of our library which can have some of its default values overriden when being used in some client application.
Thoses DLL are loaded dynamically, so I cannot just give a parameter in the constructor.
I don't want to use the App.config file because the user can edit those values(otherwise it would have been just fine).
There are several different ways to solve this.
If you like the idea of config-files, but do not want to have it accessible by end users in the compiled application, perhaps you can create your own settings-file in a format that suits your needs, and include it as an embedded resource?
An upside of this would be that you can access it as a regular XML or config file or whatever in Visual Studio, while it will be hidden from the end user. Personally I think I would prefer this to using special code / classes to store config-data.
To include a file as an embedded resource, include it into one of your Visual Studio projects, right click the included file and select Properties. Now under Build Action, select Embedded Resource. When you build your project now, the file will be included internally in the produced .dll-file.
I'm sure you'll be able to find lot's of info about how to access an embedded resource from code. As an example, there are some useful examples in this SO question. Note especially this answer, which also mentions an alternative way to include a resource.
Expanding on my comment... you could just make an interface for a settings class with hardcoded values, and then make different implementations of that interface. To actually change which one to use, all you'd need to do is comment/uncomment the line that instantiates an object into your settings variable before you build the dll:
public class MainDllProject
{
ISettings m_Settings;
public MainDllProject()
{
// Change this before compiling
this.m_Settings = new DebugSettings();
//this.m_Settings = new DeploySettings();
// use settings from the settings class
String setting1 = this.m_Settings.Setting1
Int32 setting2 = this.m_Settings.Setting2
//...
}
}
public interface ISettings
{
String Setting1 { get; }
Int32 Setting2 { get; }
}
public class DebugSettings: ISettings
{
public String Setting1
{ get { return "data_debug";} }
public Int32 Setting2
{ get { return 2;} }
}
public class DeploySettings: ISettings
{
public String Setting1
{ get { return "data_deploy";} }
public Int32 Setting2
{ get { return 1;} }
}
On finding "a built-in way of solving this", as you said, maybe this will be useful for you...
You can actually use the Visual Studio build configuration manager to build with different settings. Using the #If directives, you can automatically make it select which lines of code to use based on the configuration. A simple example based on the default debug configuration, which adds the "DEBUG=True" variable automatically:
public MainDllProject()
{
#If DEBUG Then
this.m_Settings = new DebugSettings();
#ElseIf
this.m_Settings = new DeploySettings();
#End if
}
You can actually make your own custom-named variables to check on just like that DEBUG one: after making a configuration, open the Project properties window, go to the Compile tab, select that specific configuration in the dropdown, and then at the bottom select "Advanced Compile Options". In there is a line "Custom constants" in which you can add such variables. For simple if-statements, you can just make a boolean like "CLIENTDEPLOY=True", and then you can use #If CLIENTDEPLOY Then in your code.

sharing a static class with a DLL in C# without passing a reference

VS2012 for desktop .net framework 4.5 normal windows forms applications, not WPF
Hello, I tried to search for an answer, but I'm not sure of the correct terminology. I've managed to break my code, and can't understand what I've done wrong. (i didn't think i had changed anything, but ...)
I have a solution which contains 2 projects. The first project is an executable program, and the second is a DLL, which is loaded at run time and used by the first project.
the first project contains a form, and a static class with public static strings in the same namespace. (and some other unconnected classes). specifically:
namespace project1_namespace
{
static class settings
{
public static string some_words = "some words in a string";
}
class dll_callback{
//.. some public methods here
}
dll_callback dllcallback; // instance is initialised in the code (not shown)
Form form;
public partial class frm_splash : Form
{
private void frm_splash_FormClosing(object sender, FormClosingEventArgs e)
{
// this function actually loads the DLL, ensuring its the last step
//... some error checking code removed for brevity
Assembly assembly = Assembly.LoadFrom("c:\dllpath\project2.dll");
Type type_init = assembly.GetType("project2_class");
object init = Activator.CreateInstance(type_init, form, dllcallback);
//... some error checking code removed for brevity
}// end method
}// end form class
}// end namespace
when the form is closing, the method shown above is called which calls the second projects class project2_class constructor.
in project 2, the DLL, there is:
namespace project2_namespace
{
// how did i get this working to reference "settings" class from project 1??
public class project2_class
{
public project2_class(project2_namespace.Form1 form_ref, object callback)
{
settings.some_words = "the words have changed";
//... some more stuff
}
}
}
Now, i was experimenting with some code in an entirely different part of project2, and VS2012 suddenly started refusing to compile stating:
error CS0103: The name 'settings' does not exist in the current context
the standard solution to this appears to be to add a reference to project2, but that would create circular dependencies because project 1 calls 2 as a DLL.
I really honestly don't think i had changed anything relevant to this, but also clearly I have.
looking at it, i cant see how project 2 would have access to a class in project 1 without a reference, but the list of arguments to the project2_class constructor doesn't include one, and I am absolutely positive that it hasn't changed (and I cant change it for backwards compatibility reasons).
would really appreciate help with this, as its been a lot of work to get this working.
as a side note, I've definitely learned my lesson about not using source control. and not making "how this works" comments instead of "what this does" comments.
may dynamic help you? You can not get the setting string at complie time.

Can't load model using ContentTypeReader

I'm writing a game where I want to use ContentTypeReader. While loading my model like this:
terrain = Content.Load<Model>("Text/terrain");
I get following error:
Error loading "Text\terrain". Cannot find ContentTypeReader
AdventureGame.World.HeightMapInfoReader,AdventureGame,Version=1.0.0.0,Culture=neutral.
I've read that this kind of error can be caused by space's in assembly name so i've already removed them all but exception still occurs.
This is my content class:
[ContentTypeWriter]
public class HeightMapInfoWriter : ContentTypeWriter<HeightmapInfo>
{
protected override void Write(ContentWriter output, HeightmapInfo value)
{
output.Write(value.getTerrainScale);
output.Write(value.getHeight.GetLength(0));
output.Write(value.getHeight.GetLength(1));
foreach (float height in value.getHeight)
{
output.Write(height);
}
}
public override string GetRuntimeType(TargetPlatform targetPlatform)
{
return
"AdventureGame.World.Heightmap,AdventureGame,Version=1.0.0.0,Culture=neutral";
}
public override string GetRuntimeReader(TargetPlatform targetPlatform)
{
return
"AdventureGame.World.HeightMapInfoReader,AdventureGame,Version=1.0.0.0,Culture=neutral";
}
}
Does anyone meed that kind of error before?
I have been encountering the same problem for a week, and finally decided to do a quick check on the whole "assembly" part.
I found a fix!
Essentially, when you go into AssemblyInfo.cs, you will see all the properties(ie Title, Description, etc.)
The Title, sadly made by XNA, is NOT what your runtime reader refers to. Its actually getting the initial name of the project, which it uses to track back to your application (exe) file in your project. Try either re-making your project from scratch, making sure to keep your namespace the same as your project and never change it , or give a go at re-naming your exe file, found in the debug/obj folder in your project(i believe). hope I helped!
-Will

Categories

Resources