以编程方式从T4访问PropertyGroup中定义的C#项目定制属性

时间:2016-04-21 13:35:14

标签: c# visual-studio visual-studio-2015 t4

我在C#project的.csproj文件中定义了自定义属性:

public class BackgroundTask extends AsyncTask<String,Void,String>
{

private Context context;
private Activity activity;
private String reg_url = "http://blaah.com/register.php";
private String login_url = "http://blaah.com/login.php";
private AlertDialog.Builder builder;
private ProgressDialog progressDialog;


public BackgroundTask(Context context)
{

    this.context = context;
    activity = (Activity) context;
}

@Override
protected void onPreExecute()
{
    builder = new AlertDialog.Builder(activity);
    progressDialog = new ProgressDialog(context);
    progressDialog.setTitle("Please wait");
    progressDialog.setMessage("Connecting to Server...");
    progressDialog.setIndeterminate(true);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
protected String doInBackground(String... params)
{

    String method = params[0];

    if (method.equals("register"))
    {

        try
        {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String name = params[1];
            String email = params[2];
            String username = params[3];
            String password = params[4];
            String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                    URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                    URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null)
            {

                stringBuilder.append(line).append("\n");

            }


            httpURLConnection.disconnect();
            Thread.sleep(8000);
            return stringBuilder.toString().trim();

        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

    }
    else if (method.equals("login"))
    {
        try
        {
            URL url = new URL(login_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));


            String username,password;

            username = params[1];
            password = params [2];
            String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
                    URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line = "";
            while ((line = bufferedReader.readLine()) != null)
            {

                stringBuilder.append(line + "\n");

            }


            httpURLConnection.disconnect();
            Thread.sleep(5000);
            return stringBuilder.toString().trim();


        } catch (MalformedURLException e)
        {
            e.printStackTrace();
        } catch (ProtocolException e)
        {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onProgressUpdate(Void... values)
{
    super.onProgressUpdate(values);
}

@Override
protected void onPostExecute(String json)
{
   try
   {

       progressDialog.dismiss();


       Log.v("JSON", json);
       JSONObject jsonObject = new JSONObject(json);
       JSONArray jsonArray = jsonObject.getJSONArray("server_response");
       JSONObject jsonobject = jsonArray.getJSONObject(0);
       String code = jsonobject.getString("code");
       String message = jsonobject.getString("message");



       if(code.equals("reg_true"))
       {
           showDialog("Sucessful registration.Thank you.Enjoy_AS!", message, code);
       }

       else if (code.equals("reg_false"))
       {
           showDialog("User Already exists", message, code);
       }

       else if (code.equals("login_true"))
       {
         
         // Please You Can Take The Username & Password And Save It For Shared Preference Then Share Preference File Any where To Use It.. 
           Toast.makeText(context, "You are logged in", Toast.LENGTH_LONG).show();
           Intent intent = new Intent(activity, SplashScreen.class);
           activity.startActivity(intent);


       } else if (code.equals("login_false"))
       {
           showDialog("Login Error", message, code);
       }


   } catch (JSONException e){
       e.printStackTrace();
   }


}

public void  showDialog(String title,String message,String code)
{

    builder.setTitle(title);
    if (code.equals("reg_true") || code.equals("reg_false"))
    {
        builder.setMessage(message);//message form server
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()

        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
                activity.finish();

            }
        });


    }

    else if (code.equals("login_false"))
    {


        builder.setMessage(message);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                EditText loginEmail, loginPassword;
                loginEmail = (EditText) activity.findViewById(R.id.email);
                loginPassword = (EditText) activity.findViewById(R.id.password);
                loginEmail.setText("");
                loginPassword.setText("");
                dialog.dismiss();

            }


        });


    }
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

}

是否可以使用T4模板访问... <PropertyGroup> <SomeMineProperty>Hi there!</SomeMineProperty> </PropertyGroup> ... 的值?

SomeMineProperty

有几十个属性,但<#@ template debug="false" hostspecific="true" language="C#" #> <#@ assembly name="System.Core" #> <#@ assembly name="EnvDTE" #> <#@ import namespace="EnvDTE" #> <#@ output extension=".txt" #> <# var project= this.FindProject(); var props = project.ConfigurationManager.ActiveConfiguration.Properties; // Same result for project.Properties #> count:<#=props.Count#> <#foreach (Property prop in props){#> <#= prop.Name #> = <#= prop.Value #> <#}#> <#+ Project FindProject() { var serviceProvider = (IServiceProvider)this.Host; var dte = (DTE)serviceProvider.GetService(typeof(DTE)); var item = dte.Solution.FindProjectItem(this.Host.TemplateFile); if (item != null && item.ContainingProject != null) return item.ContainingProject; throw new InvalidOperationException(string.Format("Cannot find project for {0} template", this.Host.TemplateFile)); } #> 不在其中。

0 个答案:

没有答案