I was given the following task and I am hoping that someone will be able to guide me in the right direction. Currently, we have code compiling in C#6. Due to varying reasons, some of my fellow coworkers are running C#4 and are unable to upgrade to C#6. I have to slightly alter the code so that it compiles for my coworkers.
In c#6, we have the following code:
using System;
using static SecGlobal.Constants;
with SecGlobal.Constants being:
namespace SecGlobal
{
public static class Constants
{
public const string CONST_DB_SERVER = "server name";
public const string CONST_MAIN_TIME_ZONE = "Eastern Standard Time";
... etc
}
}
The issue I run into is that the feature "using static" is not available in C#4. Are there any alternatives?
Just replace constant references with a fully qualified name. For instance,
using System;
using static SecGlobal.Constants;
...
string s = CONST_DB_SERVER;
...
Becoming
using System;
...
string s = SecGlobal.Constants.CONST_DB_SERVER;
Related
I am pretty new to Unity and game development so sorry if this is a stupid mistake.
I am trying to create a UI in which you enter text into a input field (TMP), the script will check the input and if it reads "password", a Debug.Log is shown in the console.
I have looked around online for any help with this but nothing seems to work.
I have got a script that I assume to work, but I am getting one major problem: On line 15 it cant find the Input Field that I am trying to reference?
The script is attached to the Input Field, I am trying to reference it from there.
Any help would be greatly appreciated... Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CheckPassword : MonoBehaviour
{
public InputField inputField;
public void Awake()
{
inputField = GetComponent<InputField>();
}
public void CheckInputField()
{
if (inputField.text == "password")
{
Debug.Log("Password Correct");
}
}
}
The problem was that due to the fact that I was using a Text Mesh Pro Input Field I had to specify that in the variable.
Instead I used: [SerializeField] TMPro.TMP_InputField inputField;
And the awake function was not necessary either.
I have a class in my project
public class LocateMe {
public void locateMe(Activity activity) {
mLocationClient = new FusedLocationProviderClient(activity);
.....
that uses FusedLocationProviderClient but I keep getting the warning "This method should only be accessed from tests or within package private scope" about the line new FusedLocationProviderClient(activity). I tried to tweak and change things without success.
Use the following way to initialize it instead:
mLocationClient = LocationServices.getFusedLocationProviderClient(getApplicationContext());
or use whatever context you got.
I hope somebody could help me on this one. I am implementing an Azure Function where I am trying to serialise an XML message into .Net object. This is the code that I am currently using:
public static void Run(string input, TraceWriter log)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(App));
// more code here....
}
public class App
{
public string DataB { get; set; }
}
However, I always got this error:
2017-01-17T12:21:35.173 Exception while executing function: Functions.ManualXmlToJson. mscorlib: Exception has been thrown by the target of an invocation. System.Xml: Identifier 'Submission#0' is not CLS-compliant.
Parameter name: ident.
I have tried with XmlAttributes, without them. I added the buildOptions:warningsAsErrors as false in project.json file but nothing happens. And to be honest, I ran out of ideas because this code is actually working in an App Console.
I guess is some parameter of something, I would really appreciate if somebody can suggest me how to fix it.
Thanks!
Your best option here will be to factor the class you're attempting to serialize into a separate class library and reference that from your function.
If you implement your App class above in a different assembly, your function code would look like the following:
#r "<yourassemblyname>.dll"
using System;
using <YourClassNamespace>;
public static void Run(string input, TraceWriter log)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(App));
}
The code above assumes a private assembly reference, where you upload your assembly to a bin folder, inside of your function folder.
You can find more about external references here: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#referencing-external-assemblies
I'm opening an issue to address the CLS compliant name so this is not as confusing:
https://github.com/Azure/azure-webjobs-sdk-script/issues/1123
Another option worth trying (which would minimize the changes you'd need to make to your code) is to use the DataContractSerializer instead. You can find more information here.
Here is a quick sample of a function using the DataContractSerializer (with your type above):
#r "System.Runtime.Serialization"
using System;
using System.Xml;
using System.Runtime.Serialization;
public static void Run(string input, TraceWriter log)
{
string xml = WriteObject(new App { DataB = "Test"});
log.Info(xml);
}
[DataContract(Name = "App")]
public class App
{
[DataMember]
public string DataB { get; set; }
}
public static string WriteObject(App app)
{
using (var output = new StringWriter())
using (var writer = new XmlTextWriter(output) { Formatting = Formatting.Indented })
{
var serializer = new DataContractSerializer(typeof(App));
serializer.WriteObject(writer, app);
return output.GetStringBuilder().ToString();
}
}
I would like to create a base class for observableObject generic enough for an observable object to derive from, but I hit some technical issue. This is an extract of the class. It is an abstract that implements interface INotifyPropertyChanged. But when I tried to use PropertySupport.ExtractPropertyName, I got compiler error saying 'PropertySupport' not exist in the current context. I am using VS2002. My intention was to create a library to host a small "framework" of my own and use it for different projects. Could anyone more well versed in the reflection point out what was wrong in my code to cause the compiler error?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace MyFramework
{
[Serializable]
public abstract class ObservableObject: INotifyPropertyChanged
{
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
var handler = this.PropertyChanged;
if (handler!=null)
{
handler(this, e);
}
}
protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
var propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
this.RaisePropertyChanged(propertyName);
}
protected void RaisePropertyChanged(String propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
}
}
The error you are getting usually refers to a missing using directive or missing reference.
Looking at MSDN for the function you are trying to use it looks like you are missing the using directive Prism.ViewModel
using Microsoft.Practices.Prism.ViewModel;
If this doesn't fix your problem then you need to add a reference to the correct dll
Microsoft.Practices.Prism.Composition.dll
I've never used Prism but after copying your class, adding the correct reference & using directive it built ok.
Note: All sample code is greatly simplified.
I have a DLL defined as:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
namespace RIV.Module
{
public interface IModule
{
StringWriter ProcessRequest(HttpContext context);
string Decrypt(string interactive);
string ExecutePlayerAction(object ParamObjectFromFlash);
void LogEvent(object LoggingObjectFromFlash);
}
}
Now, outside of my solution, other developers can define concrete classes and drop them into the BIN folder of my app. Maybe something like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RIV.Module;
namespace RIV.Module.Greeting
{
public class Module : IModule
{
public System.IO.StringWriter ProcessRequest(System.Web.HttpContext context)
{
//...
}
public string Decrypt(string interactive)
{
//...
}
public string ExecutePlayerAction(object ParamObjectFromFlash)
{
//...
}
public void LogEvent(object LoggingObjectFromFlash)
{
//...
}
}
}
Now, in my app I would need to know that a new Module was available (I am guessing via web.config or something along those lines) and then be able to call it based off of some trigger in the database Campaign table (which maps to the module to use for that specific campaign).
I am trying to instantiate it this way:
var type = typeof(RIV.Module.Greeting.Module);
var obj = (RIV.Module.Greeting.Module)Activator.CreateInstance(type);
However, the compiler belches because a reference was never set to RIV.Module.Greeting.dll!
What am I doing wrong?
You need to use more reflection:
Load the assembly by calling Assembly.Load
Find the type by calling someAssembly.GetType(name) or searching someAssembly.GetTypes()
Pass the Type instance to Activator.CreateInstance
Cast it to your interface.
Instead of typeof(RIV.Module.Greeting.Module), try using
var type = Type.GetType("RIV.Module.Greeting.Module, RIV.Module.Greeting");
(i.e. load the type by specifying its assembly-qualified name as string) and casting to IModule.
This approach requires you to know the exact class and assembly names of the modules (as you wrote, they could be stored in web.config).
Alternatively, you could go for a completely dynamic plugin approach:
establish a convention that all module assemblies should be named "RIV.Module.XYZ"
scan the bin directory for matching DLLs
for each DLL, load it (e.g. Assembly.Load) and scan for types implementing IModule
instantiate all found types and cast to IModule